0

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];
    }

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Yes, I think you're right, so something like [codeArray1 objectAtIndex:indexPath.row], but where exactly?
But isn't it better to do something similar to what I have, because I heard that with so many cells (around 5000), it is easy for it to go out of bonds this way.
@Jon, no. UITableView caches its cells when they go out of the visible area and reuses them when possible for different rows. So in practice it does not matter how many rows table has, the number of cell objects created will be "number of visible rows" + 2 or so
That way we won't need to create 5000 cells which would have been too expensive
Thanks, do you know, based on how I setup the array, how to sort by code property?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.