Search code examples
core-datansfetchedresultscontrollernsindexpath

Fetched results in a section crash


I am using core data to fetch an entity. I only want those results to show up in the second section of my tableview and something else show up in another section... My app isnt crashing, but the fetched data is not showing up in the table view... I also am sure I am fetching the data correctly.

Here is some code.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

if (indexPath.section==0){
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text = _team.teamName;
            break;
        case 1:
            cell.textLabel.text = _team.headCoach;
            break;
        default:
            break;
    }

}

if (indexPath.section ==1) {
    Player *p = [_fetchedResultsController objectAtIndexPath: indexPath];
    cell.textLabel.text = p.firstName;
    cell.detailTextLabel.text = p.team.teamName;
}       

return cell;

}

Solution

  • There are a couple of issues, first you should only have one section, so you don't need to access the sections properties. So try this

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        switch(section){
            case 0:
                return 7;
            case 1:
                return [self.fetchedResultsController.fetchedObjects count];
        }
        return 0;
    }
    

    secondly, you have an issue with where you are using the following code:

    Player *p =[_fetchedResultsController objectAtIndexPath: indexPath];
    

    It is causing the problem because you are calling it for both sections and your fetch has only one section.

    To fix the crash, either wrap it with a conditional that checks for the correct indexPath.section or place it inside your switch/case statement for section 1. You can do something like this:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }
    
        if (indexPath.section==0){
            switch (indexPath.row) {
            case 0:
                cell.textLabel.text = _team.teamName;
                break;
            case 1:
                cell.textLabel.text = _team.headCoach;
                break;
            default:
                break;
            }
    
        }else{
            Player *p = [self.fetchedResultsController.fetchedObjects objectAtIndex: indexPath.row];
            cell.textLabel.text = p.firstName;
            cell.detailTextLabel.text = p.team.teamName;
        }       
    
        return cell;
    
    }
    

    Good luck

    T