【问题标题】:UITableView: Why is the "number of rows" not being updated after a delete? [closed]UITableView:为什么删除后“行数”没有更新? [关闭]
【发布时间】:2012-04-26 21:29:14
【问题描述】:

这是我的代码,它应该删除选定的行。它导致应用程序崩溃,因为它声称行数不正确。我查了,不正确;表中的行已被删除,但显然行数尚未更新(代码确实被命中)......这怎么可能? (我有另一个 UITableViewController 具有确切的代码,可以工作)。我该如何解决?

if (editingStyle == UITableViewCellEditingStyleDelete) {

    //  remove row from the table
    SQLite *sql = [[SQLite alloc] init];
    [sql deleteReadingsRow: indexPath.row];

    // Delete the row from the data source
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}   

这里是确定行数的代码:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    SingletonListOfReadings  *rShareInstance = [SingletonListOfReadings sharedInstance];

    return rShareInstance.listOfReadings.count; // Return the number of rows in the section
}

【问题讨论】:

    标签: objective-c ios xcode4


    【解决方案1】:

    我发现了问题...我依赖于在 d/b 表中删除行时未重置的单例 (SingletonListOfReadings)。

    【讨论】:

      【解决方案2】:

      当您发现问题时,您应该考虑改用 NSFetchedResultsController。只需很少的工作,它就可以在添加、删除或修改行时自动更新您的表。一般来说,它比依赖于保持单例同步更可靠。有很多将它与表格一起使用的示例。

      【讨论】:

        猜你喜欢
        • 2014-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-20
        相关资源
        最近更新 更多