Search code examples
objective-ccore-datatableviewnsmutableset

Populating TableView with NSMutableSet


I am using core data and trying to populate a UITableView with an NSMutableSet. I have two entities, Teams and Players. On my addTeamsController I am saving a player to the team as follows

-(void)saveButtonWasPressed {
    self.team =[NSEntityDescription insertNewObjectForEntityForName:@"Team" inManagedObjectContext:self.managedObjectContext];

    Player *newPlayer = (Player *)[NSEntityDescription insertNewObjectForEntityForName:@"Player" 
                                                                inManagedObjectContext:self.managedObjectContext];
    team.schoolName = _schoolName.text;
    team.teamName = _teamName.text;
    team.teamID = _teamName.text;
    team.season =  _season.text;
    team.headCoach = _headCoach.text;
    team.astCoach = _assistantCoach.text;


    [self.team addPlayers:_tempSet];

    [self.managedObjectContext save:nil];
    [self.navigationController popViewControllerAnimated:YES];    
}

On another viewController I am trying to populate a tableview with that teams players. To do that I am doing as follows

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
_array = [[_team.players allObjects] sortedArrayUsingDescriptors:sortDescriptors];

and then on my cell for row and index path I am doing the following

    cell.textLabel.text = [_array objectAtIndex:indexPath.row];

And I get the error

[Player  isEqualToString:]: unrecognized selector sent to instance

I am wondering what the best approach to filling the tableview sorted by the players first names is.


Solution

  • The best way to populate a table from a core data store is to use an NSFetchedResultController. But that is not going to fix the problem you're having, the problem is that your trying to set cell.textLabel.text to an NSManagedObject, which doesn't work. You can set

    cell.textLabel.text = Player.someStringAttribute