0

我有一个从 Twitter 搜索中获取 JSON 数据的 UITableView。我正在使用轮询来自动刷新视图。

在我的 viewDidLoad 中,我有:

[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(autoTweets:) userInfo:nil repeats:YES];

然后我有:

-(void) autoTweets : (NSTimer *)theTweets
{
    NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=%23hatlive%20-%22RT%20%40%22"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    connection = [NSURLConnection connectionWithRequest:request delegate:self];


    if (connection)
    {
        webData = [[NSMutableData alloc] init];
    }

    [[self myTableView]setDelegate:self];
    [[self myTableView]setDataSource:self];
    array = [[NSMutableArray alloc] init];
}

这样做会每 10 秒刷新一次数据。问题是,如果我在代码执行的第 10 秒滚动,应用程序就会崩溃。如果我不滚动,它会更新得很好。有谁知道为什么?

4

3 回答 3

1

尝试[self.tableView beginUpdates];[self.tableView endUpdates];同时更新您的数据。此外,您不必一直重置数据源和委托。您甚至可以调用[self.tableView reloadData];.

于 2013-01-17T22:49:30.550 回答
1

Assuming array is the data used by the table view's data source, then the problem is that you change the contents of the array at the start of the connection. Then the contents get updated as the connection completes in the background. Meanwhile, as the table is scrolling, it is still trying to access the old contents of the array.

One solution is to setup a temporary array used to deal with the new connection. Don't update the old array with the new array until just before you call reloadData on the table.

于 2013-01-17T22:49:40.360 回答
0

您可以在刷新之前禁用表格视图的触摸,然后在设置数据后启用。

self.myTableView.userInteractionEnabled = YES; // before refresh
self.myTableView.userInteractionEnabled = NO;  // after 
于 2016-04-29T16:22:36.213 回答