iphone - NSOperationQueue 和 UITableView

标签 iphone objective-c ios uitableview nsoperationqueue

我正在从 SQLite 数据库加载一些数据,以便在 UITableView 中显示。为了确保用户不被阻止并且表能够快速显示,我创建了一个队列并添加要在后台执行的数据库操作。

添加效果很好并且工作正常,但由于某种原因,随机某些行未使用数据更新!

我知道数据设置正确,因为当我单击该行时,该行会使用我的数据进行更新,但我似乎找不到更新 UI 的方法!我已经尝试了所有方法,包括使用 setNeedDisplay 等,但没有任何效果!

这是我的代码:

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

    UITableViewCell* cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.textAlignment = UITextAlignmentRight;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }

    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.textLabel.text = @"Loading ...";
    cell.tag = indexPath.row;

    NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self 
                                                                     selector:@selector(setCellData:) 
                                                                       object:cell];

    [queue addOperation:op];
    [op release];

    return cell;
}


- (void) setCellData:(UITableViewCell *)cell{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    sqlite3_stmt            *statement2;

    NSString *sqlStr = [NSString stringWithFormat:@"SELECT * FROM poemdata WHERE catid='%d' GROUP BY poemid LIMIT 1 OFFSET %d",catId,cell.tag];
//  NSLog(@"%@",sqlStr);
    sqlite3_prepare_v2(database, [sqlStr cStringUsingEncoding:NSASCIIStringEncoding], -1, &statement2, nil);

    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if(sqlite3_step(statement2) == SQLITE_ROW){
        cell.textLabel.text = [[NSString alloc] initWithUTF8String: (char *)sqlite3_column_text(statement2, 3)];
        cell.tag = sqlite3_column_int(statement2, 6);
    }else{
        cell.textLabel.text = @"Error";
    }
    sqlite3_finalize(statement2);

    [pool release];
}

最佳答案

您的逻辑有缺陷,我认为您将体验到糟糕的性能以及已经遇到的问题。最好启动一个整体操作来加载所有数据或至少部分数据,然后要求表在完成时重新加载。

您现在执行此操作的方式是,每个单元格都会访问数据库并选择一行,而不是在任何地方缓存此数据(这意味着如果它滚动到 View 之外然后再次返回,则必须再次访问数据库) 。此外,当操作完成时,不会在此单元格上调用绘图代码(这就是为什么在单击它导致绘图调用之前它不会显示)。

您应该将所有这些数据加载到自定义对象数组中。加载后,您应该重新加载表格并使用新填充的数组来设置单元格中的属性。

关于iphone - NSOperationQueue 和 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5343031/

相关文章:

iphone - 在 iphone 4 中使用特定字体,但在 3.2 版本之前不使用

iphone - 将查询字符串解析为结构化 NSDictionary

ios - CMSampleBufferSetDataBufferFromAudioBufferList 返回错误 12731

iphone - 集成iOS6 facebook功能

android - 像 Google Play 一样的 iOS Alpha 测试

iphone - 如何更改 iphone CATiledLayer fadeDuration?

android - 使用 React Native 访问 Android 上的相机和 CameraRoll?

ios - 按下按钮后立即停止声音并立即开始

iphone - 如何从 google place API 中的地点 ID 查找纬度和经度

objective-c - 使用 NSUrlRequest 上传文件导致零字节文件大小