I’m trying to check a row in a tableView without relying on indexPaths. This is similar to a question that I’ve asked before but this seems like it should be easier than it is.
I have an array of static values that is the data source for my tableView, call it fullArray. When a row is selected it’s value is placed in another array – lets call it partialArray. Before when I was doing this with indexPaths I’d iterate over the partialArray with this:
for(NSIndexPath * elem in [[SharedAppData sharedStore] selectedItemRows]) {
if ([indexPath compare:elem] == NSOrderedSame) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Works like a charm. However, now I’m trying to do this with the values in the partial array and I’m having troubles.
Here’s how I think it should work in my cellForRowAtIndexPath method in sudo code:
For every string in the fullArray, If it’s in the partialArray get it’s indexPath and check it.
Code I’ve started to cobble together:
for(NSString *string in fullArray) {
if (partialArray containsObject:string) {
//Need help here. Get the index of the string from full array
fullArray indexOfObject:string];
//And check it.
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
Doesn’t seem like it should be so hard but I can’t wrap my head around it.
I don’t know why you would switch away from storing index paths, but that’s your call. Also, you might want to use an
NSMutableSetto store your checked items instead of an array. And a better variable name would be, for example,checkedItemsinstead ofpartialArray.Anyway, if you just need to loop over the elements of
fullArrayand get each element’s index, you can use one of two approaches. One way is to just use a plain old C loop, like aforstatement:The other way is to use the
enumerateObjectsWithBlock:method: