我正在从 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];
}