清除 UITableView 缓存 [英] Clear UITableView Cache

查看:32
本文介绍了清除 UITableView 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 UITableView 数据源根据用户操作在后台线程中更新.我正在前台重新显示单元格.问题是如果新数据中的行数小于以前数据中的行数,我仍然可以看到在新行之后列出的旧行.

My UITableView data source gets updated in background thread based on user action. And I am reDisplaying cells in foreground. Problem is if number of rows in new data is less than the number of rows in previous data, I can still see old rows listed after the new rows.

15-20 秒后,这些旧行会自动删除(有时会更快).或者,如果我转到不同的屏幕并返回到我的 tableview(数据源不变),则旧行不再存在.我的猜测是有一些缓存需要强制删除.任何想法如何做到这一点?

After 15-20 seconds these old rows are automatically deleted (sometimes it is faster). Or if I go to different screen and comeback to my tableview (with the datasource unchanged), the old rows are no more there. My guess is there is some cache that needs force deletion. Any ideas how to do it?

这是我的代码快照

    dispatch_async(queue, ^{
    LoadingCompleted = NO;
    if ([lbl.text isEqualToString:@""])
    {
        [self getData:month Year:year];
        LoadingCompleted = YES;
    }
     dispatch_async(dispatch_get_main_queue(), ^{
        if (LoadingCompleted) {
            mainDataSource = mainDataSourceCopy;
            [self.tableView  reloadData];
        }
    );

});

我在 tableview 中得到的奇怪输出:
新行 1
新行 2
旧排3
旧排4
旧行 5

Weird output that I am getting in my tableview:
New Row 1
New Row 2
Old Row 3
Old Row 4
Old Row 5

推荐答案

不得在后台线程中更改数据源数组,因为它可能在修改期间从主 UI 线程访问.

You must not change the data source array in a background thread, because it may be accessed from the main UI thread during the modifications.

你应该更新改为在后台线程中创建一个单独的数组,然后将其分配给数据主线程上的源数组:

You should update a separate array in the background thread instead, and then assign it to the data source array on the main thread:

// ... compute newArray ...

dispatch_async(dispatch_get_main_queue(), ^{
    // Update data source array and reload table view.
    self.dataSourceArray = newArray;
    [self.tableView reloadData];
});

这篇关于清除 UITableView 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆