问题描述
我添加了一个无限滚动功能,并意识到每当我重新加载uitableview时,视图都会闪烁......我不知道如何修复此时的闪烁。请帮助,谢谢!
I added an infinite scrolling feature and realized that whenever I reload the uitableview, the view flickers..I am not sure how to fix the flickering at this point. Please help, thanks!
我确实看到了一个非常相似的问题,但没有解决方案。我尝试了作者发布的那个,但它不起作用:从父视图中删除tableview,重新加载数据,将表视图放回到父视图中()
I did see a very similar question, but no solution to it. I tried the one the author posted, but it doesn't not work: "remove the tableview from parent view, reload data, put table view back into parent view" (UITableView reloadData - how to stop flicker)
代码:
[ self.tableView reloadData];
Code: [self.tableView reloadData];
推荐答案
正如您所猜测的那样,调用[self.tableView reloadData]会导致闪烁;快速反复,特别是在iOS 5.x设备上。但是你可能不想重新加载整个表,你只想更新表格的可见单元格中的视图。
As you have guessed, flickering is caused by calling [self.tableView reloadData]; rapidly and repeatedly, especially on iOS 5.x devices. But you probably do not want to reload the entire table, you want to update just the view within the visible cells of the table.
假设你想要更新每个单元格一个表反映最新的下载%正在下载文件。在我的示例中调用了一个方法[downloads:totalRead:totalExpected:],在下载字节时非常快。
Let's say you want to update each cell of a table to reflect the latest download % as a file is downloading. A method [downloading:totalRead:totalExpected:] gets called in my example, extremely rapidly as bytes are downloading.
这是 NOT 到do ...在每次小更新时重新加载表(在此示例中,开发人员可能依赖cellForRowAtIndexPath或willDisplayCell方法执行所有可见单元格的更新):
This is what NOT to do... reload the table on every little update (in this example, the developer may rely on "cellForRowAtIndexPath" or "willDisplayCell" methods perform the updating of all the visible cells):
- (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
// Bad! Causes flickering when rapidly executed:
[self.tableView reloadData];
}
以下是更好的解决方案。当需要更新单元格的视图时,通过仅从表中获取可见单元格来查找该单元格,然后直接更新该单元格的视图而不重新加载表格:
The following is a better solution. When a cell's view needs to be updated, find that cell by grabbing only the visible cells from the table, and then update that cell's view directly without reloading the table:
- (void)downloading:(PPFile *)file totalRead:(long long)read totalExpected:(long long)expected {
NSArray *cells = [self.tableView visibleCells];
for(MYCellView *cell in cells) {
if(cell.fileReference == file) {
// Update your cell's view here.
}
}
}
这篇关于用新数据重新加载uitableview导致闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!