iphone - UITableView 删除行并显示 'no content' 行

标签 iphone ios uitableview

我有一个包含书签的 UITableView。当不存在书签时,我想显示默认的“无书签”行。

书签是可编辑的。当删除最后一个时,书签行将替换为默认的“[无书签]”行。但 Cocoa Touch 非常不喜欢这种情况,并且由于内部断言删除后的行数应该比之前少而崩溃。

当我在下面的屏幕截图中按删除时

enter image description here

应用程序崩溃并出现以下错误:

Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), 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).

而不是显示这个:

enter image description here

如有任何建议,我们将不胜感激。

最佳答案

虽然 @Wain 建议您同时删除一行并插入一行,但我想补充一点,您也可以使用单元格重新加载来达到相同的效果。 reloadRowsAtIndexPaths:withRowAnimation: 函数有时会被忽视,但当您需要用另一种类型的单元格替换单元格时,它非常方便。

实际上,当 TableView 通知您用户删除时,您会执行以下操作:

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        //Delete the item from the data source first!

        //If there are no more items in the data source array for that section, reload the last remaining row. Otherwise, just delete the row.
        if ([myDataSourceArrayForTheAppropriateSection count] == 0)
            [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
        else
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }
}

就像我说的,这是一种对我来说更有意义的替代解决方案,而且它还有只使用一次动画调用的好处。

希望这有帮助!

关于iphone - UITableView 删除行并显示 'no content' 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18242200/

相关文章:

ios - “strcmp”未在此范围内声明

ios - 尝试在 ios 中保存大量数据时出现内存问题

ios - 如何在包含五个按钮的单个 ViewController 中使用五个 UITableViews。第一个按钮到第一个 tableview..so on

ios - 在 iOS 的 UItableview 中将行推到顶部

iphone - 将数据从委托(delegate)传递到 UIViewController

iphone - 如何在 iOS(iPhone、iPad)项目中使用 CARingBuffer 类?

iphone - 设置一次后如何去除UIButton的渐变效果 - iPhone

iphone - uiwebview 和 mobile safari 的区别

iphone - 结合 GLKView 与 UIBUtton

ios - 变量总是按值传递