Note that there are some explanatory texts on larger screens.

plurals
  1. POChanging accessoryType for a row in a sectioned tableView after user selects the row
    primarykey
    data
    text
    <p>I have a sectioned tableView that I want my user to select one item from the table. When they select the item, a check should appear next to the item (using <code>UITableViewCellAccessoryCheckmark</code>). If they had made a previous selection, the check should be removed from the previously selected row. Here is the code I am using:</p> <pre><code>-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { int newRow = [indexPath row]; int oldRow = [lastIndexPath row]; if (newRow != oldRow || newRow == 0) { UITableViewCell *newCell = [tableView cellForRowAtIndexPath:indexPath]; newCell.accessoryType = UITableViewCellAccessoryCheckmark; UITableViewCell *oldCell = [tableView cellForRowAtIndexPath: lastIndexPath]; oldCell.accessoryType = UITableViewCellAccessoryNone; [lastIndexPath release]; lastIndexPath = indexPath; } [tableView deselectRowAtIndexPath:indexPath animated:YES]; } </code></pre> <p><code>lastIndexPath</code> is declared privately in the <code>.h</code> file. </p> <p>This code works great for a small list that is not sectioned. But in a large table that is sectioned, it puts random check marks in rows in other sections. It is almost as if the <code>cellForRowAtIndexPath</code> is ignoring the section in indexPath.</p> <p>The code also crashes if I select a row that is greater than the number of rows in the smallest section.</p> <p>Here is the code for cellForRowAtIndexPath:</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString *key = [keys objectAtIndex:section]; NSArray *itemSection = [items objectForKey:key]; static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SectionsTableIdentifier] autorelease]; } NSArray *rowLabel = [itemSection objectAtIndex:row]; cell.textLabel.text = [rowLabel objectAtIndex:1]; NSString *detText = [rowLabel objectAtIndex:0]; detText = [detText stringByAppendingString:@" $"]; detText = [detText stringByAppendingString:[rowLabel objectAtIndex:2]]; cell.detailTextLabel.text = detText; return cell; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload