Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Setting the accessory view needs to happen <em>inside</em> the <code>tableView:cellForRowAtIndexPath:</code> method. When you want to change the accessories from outside, the outside method needs to change the model first to indicate that check marks must be placed in certain cells, and then call <code>reloadData</code> on the <code>UITableView</code>.</p> <p>One way to store what cells are checked is an array of <code>NSIndexSet</code> objects - one index set per section. In the example below I show code for a single section, but you should get an idea of how to make multiple sections work.</p> <pre><code>// This variable needs to be declared in a place where your data source can get it NSMutableIndexSet *selected; // You need to initialize it in the designated initializer, like this: selected = [[NSMutableIndexSet alloc] init]; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } if ([selected containsIndex:indexPath.row]) { [cell setAccessoryType:UITableViewCellAccessoryCheckmark]; } else { [cell setAccessoryType:UITableViewCellAccessoryNone]; } // Do the rest of your code return cell; } </code></pre> <p>Now in the code where you want to set rows selected or unselected you just need to call <code>[selected addIndex:rowToSelect]</code> or <code>[selected removeIndex:rowToUnselect]</code>, and call your table's <code>reloadData</code>.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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