2011-07-09 45 views
0

我被困在一個問題中,我有一個可以讓行說5的行。 如果用戶選擇一行,那麼應該創建一個恰好在被點擊行下方的新行/插入一個動畫(正如我們在部分隱藏/取消隱藏中看到的那樣),並且在點擊新插入的行時它應該被刪除。在uitableview的同一部分插入/創建新行

我已經給了一個嘗試,但它說

 

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid 
update: invalid number of rows in section 0. The number of rows contained in an existing section 
after the update (6) must be equal to the number of rows contained in that section before the update 
(5), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).' 
 

所以應該是什麼其他的方式來實現這一功能? 在此先感謝。

回答

0
 

- (NSInteger)numberOfRowsInSection:(NSInteger)section { 
    switch (section) { 
     case 0: 
      return numberOfRows; 
    } 
    return 0; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    numberOfRows ++; 
    [tableView deselectRowAtIndexPath:indexPath animated:NO]; 
    NSMutableArray* tempArray = [[NSMutableArray alloc] init]; 
     [tempArray addObject:[NSIndexPath indexPathForRow:indexPath.row +1 inSection:indexPath.section]]; 
    [tableView beginUpdates]; 
    [tableView insertRowsAtIndexPaths:tempArray withRowAnimation:UITableViewRowAnimationRight]; 
    [tableView endUpdates]; 
    [tempArray release]; 

} 


 

我正在2個錯誤 1)I wasnot使用[的tableView beginUpdates]顯然[的tableView endUpdates]更新用 2)來計算一NEWROW的indexpath的方法之後是ambigious。

三江源非常pratikshabhisikar和最大霍威爾對把你的時間和努力。

2

最初你有5行。你添加一個新的行到表中,讓我們說使用addRowsAtIndexPaths:方法。此時,你的表視圖將調用它的數據源方法,因爲它需要添加這個新的單元。

但是,可能是你的還是返回的行數爲5(而不是6),從數據源的方法,這導致不一致(如表視圖預計6行,你還在返回5行)

所以,讓我們說,當表視圖要求的cellForRowAtIndexPath:新創建的細胞的方法(行= 5),它可能會崩潰,因爲你必須做一些如:

[yourDatasourceArray objectAtIndex:indexPath.row]; 

上述聲明將導致崩潰,因爲indexPath .row是5,你的數組裏仍然有5個對象(索引0到4)。因此,objectAtIndex:5導致崩潰。

+0

這是最有可能的答案。總之,在調用'insert'或'delete'之後,從'numberOfRows'返回正確的數字。 – mxcl

+0

嘿傢伙...感謝您的意見...我已經整理出了問題...請在下面找到我的答案 – yunas

相關問題