views:

356

answers:

3

This

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

[tableView deselectRowAtIndexPath:indexPath animated:YES];

}

... modifies the "accessoryType" of every 6th cell INSTEAD of just the selected row. What am I missing?

Thanks

UPDATED: Here is the cell creation code ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *TC = @"TC";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:PlayerTableViewCell] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1];

    return cell;
}

MY SOLUTION based on the marked answer below is ...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // EVERY row gets its one Identifier
    NSString *TC = [NSString stringWithFormat: @"TC%d", indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: TC];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TC] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [NSString stringWithFormat:@"Person %d", row+1];

    return cell;
}

If there is a better way I'm all ears. Would be nice if we could just change the SPECIFIC Cell according to the NSIndexPath passed in someday (at least that seems a whole lot more intuitive to me).

A: 

Just tried your code - it works fine for me. Could you please provide more details, like cell creation code, etc?

kovpas
See updated question above. Also, this only shows up when i have 7 or more cells in the table (e.g. I click row 1 and both it and row 7 get a checkmark).
wgpubs
Ha! You are right. In my case it's +10th cell. Trying to figure out...
kovpas
I think it has something to do with how UITableView reuses cell. The code in "didSelectRowAtIndexPath" returns a "cell" ... which I think is a pointer to all rows pointing to that particular instance. Either way ... its a bit confusing and not sure how to update JUST the selected row.
wgpubs
+2  A: 

Yeah, just figured out. These are the same cells ( logs below are NSLog( @"%d %@", row, cell ) ).

2010-04-15 12:43:40.722 ...[37343:207] 0   <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 0; 320 44); autoresize = RM+TM; layer = <CALayer: 0x124c3a0>>

2010-04-15 12:43:47.827 ...[37343:207] 10   <MyListCell: 0x124bee0; baseClass = UITableViewCell; frame = (0 31; 340 44); autoresize = W; layer = <CALayer: 0x124c3a0>>

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: PlayerTableViewCell];

dequeueReusableCellWithIdentifier uses already created cell, if the previous one is out of a screen.

So, you should avoid using "dequeueReusableCellWithIdentifier" - create a dictionary NSIndexPath -> UITableViewCell and use it instead of dequeueReusableCellWithIdentifier.

ADDED: You can also check if the (rowNumber+1) exists in a winningNumbers array and replace accessory in a tableView:cellForRowAtIndexPath: method. This should work too

kovpas
Isn't that against best practice? And could that start having some really negative performance ramifications as the number of rows grow?
wgpubs
Ok, another way:check if the (rowNumber+1) exists in a winningNumbers array and replace accessory in a cellForRowAtIndexPath method. This should work too.
kovpas
A: 

I got it to work by using setting the accessory type in cellForRowAtIndexPath based on variable which keeps track of the selected row. Then in didSelectRowAtIndexPath i just deselect the row, update the variable, and reload the table.

CODE SAMPLE:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [NSString stringWithFormat:@"%d", indexPath.row];

    if ([_cells indexOfObjectIdenticalTo:_selectedCell] == indexPath.row) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    _selectedCell = [_cells objectAtIndex:indexPath.row];
    [tableView reloadData];
}
marchinram