Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In reaction to the user's swipe you should store the user's choice (e.g. in a private instance variable of type <code>NSMutableArray</code>). When the user comes back to the table view you can then reuse the information in your <code>tableView:cellForRowAtIndexPath:</code> to setup the cell with the correct accessory style.</p> <p>Property declaration:</p> <pre><code>@property(nonatomic, retain) NSMutableArray* _accessoryStyle; </code></pre> <p>Synthesize the property. Then add this snippet to the bottom of <code>handleSwipeLeft:</code> to store the user's choice:</p> <pre><code>- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)gestureRecognizer { [...] NSNumber* number = [numberWithInt:0]; [_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number]; } </code></pre> <p>Add a similar snippet to the bottom of <code>handleSwipeRight:</code>:</p> <pre><code>- (void)handleSwipeRight:(UISwipeGestureRecognizer *)gestureRecognizer { [...] NSNumber* number = [numberWithInt:1]; [_accessoryStyle replaceObjectAtIndex:indexPath.row withObject:number]; } </code></pre> <p>In <code>tableView:cellForRowAtIndexPath:</code>:</p> <pre><code>NSString* accessoryImageName; NSNumber* number = [_accessoryStyle objectAtIndex:indexPath.row]; switch ([number intValue]) { case 0: accessoryImageName = @"disclosure.png"; break; case 1: accessoryImageName = @"tick.png"; break; default: // replace with your error handling code return nil; } cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:accessoryImageName]]; </code></pre> <p>For all this to work you need to initialize the <code>_accessoryStyle</code> array with the same number of elements that you expect your table view to have cells. For instance, in your view controller's <code>viewDidLoad</code>:</p> <pre><code>- (void) viewDidLoad { [super viewDidLoad]; self._accessoryStyle = [NSMutableArray arrayWithCapacity:0]; NSNumber* defaultAccessoryStyle = [numberWithInt:0]; int numberOfRows = 17; // get the real number from somewhere for (int index = 0; index &lt; numberOfCells; ++index) [_accessoryStyle addObject:defaultAccessoryStyle]; } </code></pre> <p>And to balance this you need to add</p> <pre><code>- (void) viewDidUnload { [super viewDidUnload]; self._accessoryStyle = nil; } </code></pre> <p>There is still much room for improvement:</p> <ul> <li>Find better variable names</li> <li>Use an enumeration for the different styles instead of just hardcoded numbers 0 and 1</li> <li>Do not allocate a new <code>UIImageView</code> for each table view cell, just allocate two of them and use the right one depending on the accessory style</li> </ul>
    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.
 

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