Domanda

I have a custom UITableViewController which has passed to it an NSArray containing NSArrays containing NSDictionarys. For now (until I get this working) the NSDictionary objects just have "id" and "value" elements.

  • The UITableViewController uses a custom UITableViewCell which for the time being only has a label in it.

  • Also, for the sake of simplicity for the time being, all the NSDictionary objects contain only numbers for the "value" so I can track what's going on easier. So array[0][0] has "1-1", array[1][0] has "2-1" and counting up.

The Problem:

The table rows are sized such that I can only see 3 rows initially. When I scroll the 4th row is displayed correctly with "4-1". However, while following the code I noticed that the 5th row is not initialized properly.

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"] autorelease];

    NSDictionary *temp = [tmp objectAtIndex:0];
    cell.descriptionLabel.text = [temp objectForKey:@"value"];
    cell.delegate = self;
}

I noticed that the "cell" object does not come back nil and is already initialized. It displays "1-1" in the label instead of "5-1".

Anybody have any idea why? I wouldn't think it has to do with the same cell identifier names being used.

Help is much appreciated.

È stato utile?

Soluzione

Your code needs to be something like this:

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"] autorelease];

    cell.delegate = self;
}

NSDictionary *temp = [tmp objectAtIndex:indexPath.row];
cell.descriptionLabel.text = [temp objectForKey:@"value"];

I'm assuming tmp is an array and you want the value from the array based on the cell's indexPath.row.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top