1

I want to make a UITableView for selecting options (one checkmark accessory at a time), like in the Settings app (e.g. choosing the font for Notes).

I've been reading other threads, am making sure I reset the accessory type in the cellForIndexPath method, and I do deselectCell... in the didSelect... method. However, I am only get the table to "refresh" using [tableView reloadData].

Unfortunately, that cancels/cuts short the [tableView deselectRowAtIndexPath: animated:] method. Is there any way to implement this, without a raw loop throughout all the rows?

2 Answers 2

1

Try something like this:

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // In cellForRow... we check this variable to decide where we put the checkmark
    self.checkmarkedRow = indexPath.row;

    // We reload the table view and the selected row will be checkmarked
    [tableView reloadData];

    // We select the row without animation to simulate that nothing happened here :)
    [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];

    // We deselect the row with animation
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
2
  • +1 for the quick response, because it works, and seems to be exactly the same, but I want to wait to see if there are any other solutions. Just seems weird to rely on reselecting the row quickly, for behavior that's quite "native" to iOS (common action in many apps)
    – Raekye
    Jun 11, 2013 at 23:36
  • Some very common behaviors do not have a straightforward one line solution :)
    – e1985
    Jun 11, 2013 at 23:40
1

If you only allow one checkmark at a time, you can just keep the currently selected indexPath (or a suitable proxy) in a property and then you only need to update two rows.

Otherwise, you're going to have to loop. Typically, I have a configureCell:atIndexPath: method that I can call from anywhere (including cellForRowAtIndexPath) combined with a reloadVisibleCells method:

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    //cell configuration logic
}

- (void)reconfigureVisibleCells
{
    for (UITableViewCell *cell in [self.tableView visibleCells]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        [self configureCell:cell atIndexPath:indexPath];
    }
}

Or you can use the more traditional method of reloading cells in a begin/endUpdates sandwich if you want to have the built-in row animations:

- (void)reloadVisibleCells
{
    [self.tableView beginUpdates];
    NSMutableArray *indexPaths = [NSMutableArray array];
    for (UITableViewCell *cell in [self.tableView visibleCells]) {
        NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
        [indexPaths addObject:indexPath];
    }
    [self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView endUpdates];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.