• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iphone - NSOperationQueue 和 UITableView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 16:57:56 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

这是我的代码:

- (UITableViewCell *) tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)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 
                                                                     selectorselector(setCellData 
                                                                       object:cell];

    [queue addOperationp];
    [op release];

    return cell;
}


- (void) setCellDataUITableViewCell *)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];
}



Best Answer-推荐答案


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

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

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap