Clear UITableView Cache

梦想的初衷 提交于 2019-12-13 01:21:28

问题


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.

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?

Here is snapshot of my code

    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];
        }
    );

});

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


回答1:


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];
});



回答2:


Use -reloadData on the table view.

[tableView reloadData];

EDIT: I noticed that you said you were updating on a background thread, so just in case use performSelectorOnMainThread:withObject:waitUntilDone::

[tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

Or you can use GCD's dispatch_async()

dispatch_async(dispatch_get_main_queue(), ^{
    // Set new data
    [tableView reloadData];
});


来源:https://stackoverflow.com/questions/23041329/clear-uitableview-cache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!