Setting Cells' Text From An Array
I think I'm missing something very simple here. The cell's text is being set to the same thing for each cell right now. Can anyone see my error?
for (ProductItem *code in codeAray1)
{
NSString *codeText = code.code;
NSString *nameText = code.name;
NSString *cellText = [NSString stringWithFormat:@"%@: %@", codeText, nameText];
cell.textLabel.text = 开发者_运维知识库cellText;
}
And this is how the array is created from SQLite query:
ProductItem *code = [[HCPCSCodes alloc] init];
code.code = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 0) encoding:NSUTF8StringEncoding];
code.name = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 1) encoding:NSUTF8StringEncoding];
code.description = [NSString stringWithCString:(char *)sqlite3_column_text(statement, 2) encoding:NSUTF8StringEncoding];
[codeAray1 addObject:code];
[code release];
}
You set text to the same cell on each loop iteration in your 1st snippet (wherever that code is)... You need to set text based on the current row of a cell
Basically, that code should be in cellForRowAtIndexPath: method in table's data source and look like:
- (UITableViewCell*) tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath)*indexPath{
UITableViewCell *cell = ...// create/deque/setup cell
// Now get just the code that should be displayed in a given row
ProductItem *code = [codeAray1 objectAtIndex: indexPath.row];
NSString *codeText = code.code;
NSString *nameText = code.name;
NSString *cellText = [NSString stringWithFormat:@"%@: %@", codeText, nameText];
cell.textLabel.text = cellText;
return cell;
}
精彩评论