繁体   English   中英

UITableView-当上面有一定数量的单元格时,删除最后一个单元格

[英]UITableView - Remove last cell when there are a certain amount of cells above it

在我的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)

这是Apple的UITableView文档中的相关段落,该段落与插入和删除单元格有关:

单击插入或删除控件会使数据源收到tableView:commitEditingStyle:forRowAtIndexPath:消息。 您可以通过调用deleteRowsAtIndexPaths:withRowAnimation:或insertRowsAtIndexPaths:withRowAnimation:来进行删除或插入。 同样在编辑模式下,如果表视图单元格的showsReorderControl属性设置为YES,则数据源会收到tableView:moveRowAtIndexPath:toIndexPath:消息。 数据源可以通过实现tableView:canMoveRowAtIndexPath:有选择地删除单元格的重新排序控件。

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

一旦插入和删除是“承诺”,只有这样你在报到不同的号码numberOfRowsInSection方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM