1

I currently have a UIAlertview displaying a grouped table view and have spent all week getting it to work just right, i left what should have been the easiest part till last, adding the accessories. All i want is to have the row show up as checked when it is tapped. This sounds simple enough an at first glance works fine. If i tap the cell at 0,0 it gets ticked BUT, if i then scroll down i also end up with 2,1 being ticked and 3,6 (and 0,0 gets unticked again). I have looked at This Question and tested their code on another grouped table and it works fine but made no difference to my Alert Table.

This is the code i am using

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

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    cell.accessoryType = UITableViewCellAccessoryCheckmark;

}

I see no way that this can modify more than one cell. I have put a break point there and it is only ever called one time per tap.

Cellforrowatindex method:

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

      static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

      NSArray *listData =[self.data objectForKey:

      [self.sortedKeys objectAtIndex:[indexPath section]]];

      UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

     if(cell == nil) {

         cell = [[[UITableViewCell alloc]
             initWithStyle:UITableViewCellStyleDefault
             reuseIdentifier:SimpleTableIdentifier] autorelease];
      }

    NSUInteger Row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:Row];

    return cell;
}

Thanks.

Community
  • 1
  • 1
Darc
  • 745
  • 10
  • 26
  • please also share your `-tableview:cellForRowAtIndexPath:` method, since that's generating new rows. (new as in: new on the screen, not new in your table) – mvds Mar 18 '11 at 21:41
  • Added requested code. While i was doing that i think i found the issue. Is it likely that `dequeueReusableCellWithIdentifier` is reusing cells with the accessory? If so, how can i make the accessory addition stick threw scrolling, do i need to write it back to somewhere? Thanks – Darc Mar 18 '11 at 21:58
  • Well done, that's correct, there is your issue. There is no way the SDK will remember your accessories, so you will have to do that yourself, e.g. using an array or dictionary. – mvds Mar 18 '11 at 23:50

1 Answers1

1

I'm thinking that because you're reusing UITableViewCells, once you change one cell accessoryType, it may affect the rest of the cells in the table view.

Inside your cellForRowAtIndexPath after you get the cell object, do the following :

cell.accessoryType = UITableViewCellAccessoryNone

Hope it helps.

Idan
  • 5,717
  • 10
  • 47
  • 84
  • Thanks, that does fix the original issue but now if i tick one and scroll off the screen it unticks it again, do i need to save it anywhere to say "this cell is ticked"? – Darc Mar 18 '11 at 22:31
  • Yes you do, you can have an array of cell numbers which you add more cell numbers into in the didSelectRowAtIndexPath. then you should check inside cellForRowAtIndexPath if the current row exist in that array, and if so, change the accessory type accordingly. that's of course one solution, guess you can find other alternatives as well. – Idan Mar 18 '11 at 22:39