UITableView-删除部分中的最后一个单元格

发布于 2024-12-29 11:09:25 字数 1470 浏览 3 评论 0原文

这是我在 UITableView 中删除单元格的代码:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];

    if([tableView numberOfRowsInSection:[indexPath section]] > 1) {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade];
    } else {
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
            withRowAnimation:UITableViewRowAnimationFade];
    }

    [tableView endUpdates];
}

在删除单元格之前还有一些数据正在更新,但我确信它正在正确更新,因为数据源的 count每次删除一个单元格时,数组总是少一。这是预期的行为。但由于某种原因,每当我删除某个部分中的最后一个单元格时,我都会收到“无效更新:第 0 部分中的行数无效。更新 (0) 后现有部分中包含的行数必须相等”等于更新前该部分中包含的行数 (1),加上或减去从该部分插入或删除的行数(插入 0 行,删除 0 行),并加上或减去移入或移出该部分的行数部分(0 移入,0 移入出)。'

这是令人难以置信的令人沮丧,特别是因为从我在互联网上找到的信息来看,我做得正确。也许我需要睡觉,已经有一段时间了。这里有什么想法吗?

解决方案:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableIsEmpty) {
        //**this** line was the culprit- returning zero here fixed the problem
        return 0;
    } else if(section == ([tableView numberOfSections] - 1)) {
        //this is the last section- it only needs one row for the 'Delete all' button
        return 1;
    } else {
        //figure out how many rows are needed for the section
    }
}

This is my code to delete a cell in my UITableView:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView beginUpdates];

    if([tableView numberOfRowsInSection:[indexPath section]] > 1) {
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] 
            withRowAnimation:UITableViewRowAnimationFade];
    } else {
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] 
            withRowAnimation:UITableViewRowAnimationFade];
    }

    [tableView endUpdates];
}

There is also some data being updated before the cell is deleted, but I'm rather sure it is being updated properly because the count of the data source's array is always one less each time a cell is deleted. This is the expected behavior. Yet for some reason, whenever I delete the last cell in a section, I get 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (0) 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, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

It is incredibly frustrating, especially because from what I have been able to find on the Internet, I am doing this correctly. Maybe I need sleep, it's been a while. Any ideas here?

Solution:

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
    if(tableIsEmpty) {
        //**this** line was the culprit- returning zero here fixed the problem
        return 0;
    } else if(section == ([tableView numberOfSections] - 1)) {
        //this is the last section- it only needs one row for the 'Delete all' button
        return 1;
    } else {
        //figure out how many rows are needed for the section
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2025-01-05 11:09:25

您的问题在于 numberOfRowsInSection: 方法的实现。你的删除似乎没问题。当您删除行时,我认为您无法更新用于在 numberOfRowsInSection: 方法中返回行数的源。由于存在不一致,您会收到错误。请分享该代码。

Your problem lies in the implementation of your numberOfRowsInSection: method. Your deletion seems to be fine. When you've deleted the rows, I think you are failing to update the source used to return the number of rows in the numberOfRowsInSection: method. Because of the inconsistency there, you get the error. Please share that code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文