views:

27

answers:

1

I am trying to manage a drill-down list of data. I've got an entity, Contact, that has a to-many relationship with another entity, Rule. In my root view controller, I use a fetched results controller to manage and display the list of Contacts. When a Contact is tapped, I push a new view controller onto the stack with a list of the Contact's Rules.

I have not been able to figure out how to use a second fetched results controller to display the Rules, so I'm using the following:

// create a set of the contact's rules
rules = [NSMutableSet set];
rules = [self.contact mutableSetValueForKey:@"rule"];

// create an array of rules from the set
arrayOfRules = [NSMutableArray arrayWithCapacity:[rules count]];
for (id oneObject in rules)
     [arrayOfRules addObject:oneObject];

// sort the array of rules
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"phoneLabel" ascending:YES];
[arrayOfRules sortUsingDescriptors:[NSArray arrayWithObject:descriptor]];
[descriptor release];

I create a set of Rules, then use that to create an array of Rules for sorting. I then use these two collections to populate the grouped table view. All of this appears to be working correctly.

Here's my problem: There are several different actions a user can take in this view, and most of them require that I know which Rule was tapped. But I can't figure out how to get that. For instance, say a user wants to delete a Rule. It seems to me the proper approach is something like...

[rules removeObject:ruleObjectToBeRemoved]

...but I can't figure out how to specifiy ruleObjectToBeRemoved. I hope all of this makes sense.

As usual, thanks in advance for any advice you can offer.

A: 

I assume that you somehow keep the reference to the arrayOfRules. If so, when the user taps the cell method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

of tableViewDelegate should be called. If so, you can use the indexPath to get the corresponding Rule object, just like this

Rule* yourRule = [arrayOfRules objectAtIndex:indexPath.row];

Of course, if you have multiple section, you'll need to get the array corresponding to the cell tapped (use indexPath.section for this).

The easier way to do what you wish to achieve is:

[arrayOfRules removeObjectAtIndex:indexPath.row];

Hope this helps, Paul

Pawel