清除 NSMutableArray 以进行刷新

发布于 2024-12-11 01:25:50 字数 422 浏览 0 评论 0原文

我有几个 NSMutableArrays ,刷新视图时需要清除它们。但是,当我尝试使用 [array removeAllObjects]; 清除它们时,我的 tableview 由于索引超出界限错误而崩溃。我对刷新所做的只是清除数组并调用与 viewDidLoad 中相同的函数来填充表格视图。 [tableView reloadData] 直到该方法的最后一行才会被调用。

编辑:问题很可能是这样的:我使用拉动来刷新外部库,当您向上滚动并释放表格时,它会向下弹回,因此 UITableView 尝试加载下一个单元格,但它无法加载下一个单元格,因为数组已清除,并且仍在加载。

答案:从数组中删除AllObjects,立即执行self.tableView reloadData,然后继续其余操作。

I have a couple of NSMutableArrays which i need to clear when refreshing the view. However, when I try to clear them with [array removeAllObjects]; my tableview crashes due to index beyond bounds error. All i do with the refresh, is clear the arrays and call the same function as in viewDidLoad for filling the tableview. [tableView reloadData] doesn't get called until the very last line of the method.

EDIT: It's highly likely that the issue is this: I use a pull to refresh external lib, and when you scroll up and release the table, it bounces back down and thus the UITableView tries to load the next cell, which it cant because the array is cleared, and it's still being loaded.

Answer: removeAllObjects from the arrays, immediately do a self.tableView reloadData and then continue with the rest.

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

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

发布评论

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

评论(4

猫九 2024-12-18 01:25:51

问题可能是由于 numberOfRowsInSection 返回一些计数而您的数据源数组为空。

只需调用[array removeAllObjects]并在numberOfRowsInSection返回[array count]

我希望它能解决您的问题。祝你好运!!!

problem might be due to numberOfRowsInSection returning some count and your data source array is empty.

just call [array removeAllObjects] and in numberOfRowsInSection return [array count].

I hope it will resolve your issue. Best of Luck!!!

渔村楼浪 2024-12-18 01:25:51

我通过以下方式从表格视图中删除单元格 -

NSMutableArray* indexPathsToDelete = [[NSMutableArray alloc] init];
            for(unsigned int i = 0; i < [self.array count]; i++)
            {
                [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]];
            }

[self.tableView beginUpdates];
[self.array removeAllObjects];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
[indexPathsToDelete release];

I delete cells from my table view in the following manner-

NSMutableArray* indexPathsToDelete = [[NSMutableArray alloc] init];
            for(unsigned int i = 0; i < [self.array count]; i++)
            {
                [indexPathsToDelete addObject:[NSIndexPath indexPathForRow:i inSection:0]];
            }

[self.tableView beginUpdates];
[self.array removeAllObjects];
[self.tableView deleteRowsAtIndexPaths:indexPathsToDelete withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
[indexPathsToDelete release];
筑梦 2024-12-18 01:25:51

当您刷新数组时,首先检查它是否有该对象&然后重新初始化您的阵列并释放一个。

When you refresh your array, first check if it has the object or not & then reinitialize your array and release one.

只想待在家 2024-12-18 01:25:51

我所做的是

[array removeAllObjects];

然后打电话

[self.tableview reloadData];

What i did was

[array removeAllObjects];

then call

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