I am playing around with an app that keeps track of when people receive a publication. I have two core data entities, publication and person, which have to-many relationships to each other as each can have many of the other. Publication <<->> Person.
I am trying to iterate through the relationships so that if a person has received the publication the cell should be style check mark. If they haven't then it should be cell style plain. Here is what I have so far:
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Person *personForCell = (Person *) [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", personForCell.firstName, personForCell.lastName];
NSArray *issuesForPersonArray = [personForCell.issues allObjects];
if ([issuesForPersonArray count] != 0) {
for (int i = 0; i <= [issuesForPersonArray count]; i++) {
if ([issuesForPersonArray objectAtIndex:i] == km) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
When I run this it will display all persons as long as none have a relationship to this publication. But once I select their name I get this log:
Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
Here is the didSelectRow method:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
// Reflect selection in data model
Person *personSelected = (Person *) [fetchedResultsController objectAtIndexPath:indexPath];
[publication addPersonObject:personSelected];
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
// Reflect deselection in data model
Person *personSelected = (Person *) [fetchedResultsController objectAtIndexPath:indexPath];
[publication removePersonObject:personSelected];
}
}
I'm sure I'm going about this all wrong, any help would be greatly appreciated.
Figured it out. Here is what I used
-(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Person *personForCell = (Publisher *) [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", personForCell.firstName, personForCell.lastName];
NSMutableArray *issuesForPersonArray = [[personForCell.publication allObjects] mutableCopy];
if ([issuesForPersonArray count] != 0) {
for (Publication *publicationForPerson in issuesForPersonArray) {
if (publicationForPerson == publication) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
}
}