I'm loading some data from a SQLite database to be displayed in a UITableView
. To make sure the user is not blocked and the table is displayed quickly I'm creating a queue and adding the database operation to be executed in the background.
The add is good and works fine but for some reason randomly some of the rows are not updated with the data!
I know the data is set correctly as when I click on the row the row is updated with my data but I can't seem to find a way to update the UI! I've tried everything including using setNeedDisplay
and so on but nothing works!
Here is my code:
- (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];
}