刷新UITableView自定义单元格时出现延迟

时间:2012-04-10 06:55:27

标签: objective-c ios5 uitableview

我有一个带有自定义单元格的UITableView,我在解析xml数据后填充(使用数组信息服务)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            [self.cellNib instantiateWithOwner:self options:nil];
            cell = tmpCell;
            self.tmpCell = nil;          
        }       

        infoService *e = [self.infoservices objectAtIndex:indexPath.row];

        cell.name = [e  infoName];

        NSString *infodetails = [e infoDetails];

        if ( infodetails == nil ) {
            cell.details = @"Loading...";
            [self startInfoDownload:e forIndexPath:indexPath];

            NSLog(@"Loading...");
        } else  {
            cell.details = infodetails;
            NSLog(@"Show info detail: %@", infodetails );
        }
        return cell;
}


- (void)infoDidFinishLoading:(NSIndexPath *)indexPath
{
    infoDownloader *infoserv = [imageDownloadsInProgress objectForKey:indexPath];
    if (infoserv != nil)
    {

        [infoservices replaceObjectAtIndex:[indexPath row] withObject:infoserv.appRecord];

        NSIndexPath *a = [NSIndexPath indexPathForRow:indexPath.row inSection:0]; // I wanted to update this cell specifically
        ApplicationCell *cell = (ApplicationCell *)[self.tableView cellForRowAtIndexPath:a];
        cell.details = [[infoservices objectAtIndex:[indexPath row]] infoDetails];

        NSLog(@"Updating=%@", [[infoservices objectAtIndex:[indexPath row]] infoDetails]);
    }
}

对于每个单元格,我使用NSURLConnection sendAsynchronousRequest来检索和解析来自对象infoDownloader的xml数据

- (void)startDownload

每个单独的细胞。

成功解析数据后,调用infoDownloader的委托方法

- (void)infoDidFinishLoading:(NSIndexPath *)indexPath

问题在于,而

- (void)infoDidFinishLoading:(NSIndexPath *)indexPath 

在解析每个单元格后调用,我可以看到

NSLog(@"Updating=%@", [[infoservices objectAtIndex:[indexPath row]] infoDetails]);

在具有正确详细信息的调试器中,单元格不会立即刷新,但会在6或7秒后刷新。此外,不会从

调用cellForRowAtIndexPath
- (void)infoDidFinishLoading:(NSIndexPath *)indexPath 

由于某种原因,因为在infoDidFinishLoading之后没有调试输出。此外,我不明白cell.details是如何实际刷新的,因为cellForRowAtIndexPath不会被再次调用。

我已尝试使用Apple的LazyTableImages加载示例设置此功能,我已成功使用,但我不知道出了什么问题。

2 个答案:

答案 0 :(得分:0)

加载数据后,您可能需要致电reloadRowsAtIndexPaths。可能发生的是已加载单元格数据,但单元格绘图未更新。

此外,我相信您的代码可以多次请求相同的数据,因为如果[e infoDetails]nil请求,但是在加载数据之前可以多次请求该单元{{1将被多次调用,下载相同的数据。您应该考虑跟踪您请求数据的行。

请查看此代码,了解有关如何解决此问题的一些建议:https://github.com/kgn/Spectttator/blob/master/SpectttatorTest-iOS/RootViewController.m#L123

答案 1 :(得分:0)

任何影响UI的更改都必须在主线程上执行。

刷新tableView,通过更改单元格详细信息,realoading tabe视图,实现行...是影响UI的更改。

特别是,您应该使用以下命令在主线程中执行更改:

dispatch_async(dispatch_get_main_queue(), ^{
    cell.details = [[infoservices objectAtIndex:[indexPath row]] infoDetails];
}

或iOS 4之前:

cell performSelectorOnMainThread:@selector(setDetails:) withObject:[[infoservices objectAtIndex:[indexPath row]] infoDetails];
相关问题