Reputation: 143
I have a UITableView with 5 single grouped cells, allowing users to access specific further screens from each option. Users will then return to this screen and I want to be able to place a checkmark against the last selected cell and turn off any previous checkmarks.
I am trying to achieve this in didSelectRowAtIndexPath:
but can't seem to get it right.
There are a few answers available for dynamic cells but nothing for static, can anyone help with this?
Upvotes: 2
Views: 897
Reputation: 15607
There are a number of ways to manage this, but one of the easier things to do is to store references to the cells in a collection so that you can easily send messages to all the cells. For example, you could add a property like the following:
@property (strong, nonatomic) IBOutletCollection(NSArray) *cells;
@property (strong, nonatomic) IBOutletCollection(UITableViewCell) NSArray *cells;
and connect it to each of the cells in your nib file or storyboard. (If you're not using Interface Builder, drop the IBOutletCollection
, and populate the array yourself wherever you create the cells.)
Then you can manage selection as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UITableViewCell *currCell in self.cells)
{
currCell.accessoryType = UITableViewCellAccessoryNone;
}
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
Upvotes: 2
Reputation: 11
Correct syntax for IBOutletCollection is:
@property (strong, nonatomic) IBOutletCollection(UITableview) NSArray *cells;
Upvotes: 1