I am thinking of using a for loop, but was wondering if there was any built in way to accomplish this. Basically in one view I have a table view with a list of objects, the user can click as many as they want, “checking” them. When they go to the next string I want to populate another table view with the selected names.
Here is my selection method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
[tableView deselectRowAtIndexPath:indexPath animated:NO];
}
I’m thinking of writing a method that contains a for loop that goes through each value in the array that the table view is pulled from and adds only those with the correct accessory type to a new array. Any ideas?
It would be more MVC to back your selections with an NSMutableIndexSet. Otherwise, what happens if the user selects a cell, but then scrolls the cell out of the frame and it gets dequeued?
Declare a propery in your interface:
Instantiate it in viewDidLoad or somewhere similar
Then modify it in your delegate method:
In your cellForRowAtIndexPath: method, decide whether to show a checkmark or not:
Now when you want the selected objects, just
Note: when I first posted this, it was really untested. I’ve since made several changes. For example, you want to set accessoryType in the cellForRowAtIndexPath: method ONLY