2012-01-02 81 views
0

在我的UITableView中,我在底部有一个插入控件的专用单元格,以允许用户插入新行。UITableView - 当上面有一定数量的单元格时,删除最后一个单元格

UITableView

我想要做什么是删除/隐藏该细胞时,有细胞的(在这种情况下8)一定数目的在其上方。

这是我到目前为止有:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == 1) { 
     if ([sites count] == [[BrowserController sharedBrowserController] maximumTabs]) { 
      return [sites count]; 
     } else { 
      return [sites count] + 1; 
     } 
    } else { 
     return 1; 
    } 
} 

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     ... 
    } else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     NSString *newSiteAddress = [NSString stringWithString:@"http://"]; 

     [sites addObject:newSiteAddress]; 

     if ([sites count] == [[%c(BrowserController) sharedBrowserController] maximumTabs]) { 
      [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     } 

     [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
     [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; 
     [[(BookmarkTextEntryTableViewCell *)[tableView cellForRowAtIndexPath:indexPath] textField] becomeFirstResponder]; 
    } 
} 

这将导致以下异常被抛出:

2/01/12 4:28:07.956 PM MobileSafari: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (8) must be equal to the number of rows contained in that section before the update (8), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' 
*** First throw call stack: 
(0x2bc0052 0x2d51d0a 0x2b68a78 0x1cf2db 0x747518 0x75282a 0x7528a5 0x812481c 0x75e7bb 0x8b2d30 0x2bc1ec9 0x6c65c2 0x6c655a 0x76bb76 0x76c03f 0x76b2fe 0x984a2a 0x2b949ce 0x2b2b670 0x2af74f6 0x2af6db4 0x2af6ccb 0x491879 0x49193e 0x6c3a9b 0x4430 0x2db5) 

回答

0

下面是从Apple's UITableView documentation相关段落,其具有插入和删除单元格的事情:

单击插入或删除控件会导致数据源 接收tableView:commitEditingStyle:forRowAtIndexPath:消息。 您可以通过调用 deleteRowsAtIndexPaths:withRowAnimation:或 insertRowsAtIndexPaths:withRowAnimation:来执行删除或插入操作,如果适用。也在 编辑模式下,如果表视图单元格的showsReorderControl 属性设置为YES,则数据源会收到一个 tableView:moveRowAtIndexPath:toIndexPath:message。数据源可以通过 tableView:canMoveRowAtIndexPath:选择性地删除单元格的重新排序控件。

你应该创建一个名为“insertLastCell”和“deleteLastCell”新方法(或类似的规定),其中明确告诉你的表视图中插入并通过insertRowsAtIndexPaths:withRowAnimation:deleteRowsAtIndexPaths:withRowAnimation:删除最后一个单元格。

一旦插入和删除“提交”,只有然后可以在numberOfRowsInSection方法中报告不同的数字。

相关问题