Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What's happening is that UITableView is recycling UITableViewCells to save memory. This means that as you scroll down the list the UITableView is taking cells off the top of the table and reusing them to display later items so when you scroll back up they have lost state.</p> <p>You could rectify this by keeping an <code>NSMutableSet</code> of checked indexPaths. When the user checks an item, you would add its <code>indexPath</code> to this set. Then in your <code>cellForRowAtIndexPath</code> you can make sure to check the item if it's in your set of checked items.</p> <p><strong>UPDATE</strong></p> <p>Here's a rough example of how this might work:</p> <pre><code># MyTableView.h @interface MyTableView: UITableView &lt;UITableViewDataSource, UITableViewDelegate&gt; { NSMutableSet *checkedIndexPaths; } @property (nonatomic, retain) NSMutableSet *checkedIndexPaths; @end </code></pre> <p>then</p> <pre><code># MyTableView.m #import "MyTableView.h" @implementation MyTableView @synthesize checkedIndexPaths; - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Normal layout stuff goes here... // ***Add code to make sure the checkbox in this cell is unticked.*** for (NSIndexPath *path in self.checkedIndexPaths) { if (path.section == indexPath.section &amp;&amp; path.row == indexPath.row) { // ***We found a matching index path in our set of checked index paths, so we need to show this to the user by putting a tick in the check box, for instance*** } } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { // Normal stuff to handle visual checking/unchecking of row here // Lazy-load the mutable set if (!self.checkedIndexPaths) checkedIndexPaths = [[NSMutableSet alloc] init]; // If we are checking this cell, we do [self.checkedIndexPaths addObject:indexPath]; // If we are unchecking, just enumerate over the items in checkedIndexPaths and remove the one where the row and section match. } @end </code></pre> <p>This is all just skeleton code and isn't tested, but hopefully it gives you the jist.</p>
    singulars
    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.
    3. 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