Only select one row with checkmark, based on Core data objects

I need to only have one row in a section selected with a checkMark. I use a NSFetchresultscontroller to retrieve data from by core data store. Here I have an entity that has a selected flag. Currently I have the following code in my didSelectRowAtIndexpath method.

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

[tableView deselectRowAtIndexPath:indexPath animated:NO];

if (![indexPath section] == 0) {

    NSIndexPath *frcIndexPath = [NSIndexPath indexPathForRow:indexPath.row inSection:(indexPath.section - 1)];
    SubMenus *subMenus = [self.fetchedResultsController objectAtIndexPath:frcIndexPath];

    // Toggle 'selected' state
    BOOL isSelected = ![self subMenuIsSelected:subMenus];   //![self cellIsSelected:indexPath];  

    if ([subMenus.group.maxSelectCount isEqualToNumber:@(1)]) {

        DLog(@"Only Select One row in this group/section");
    }else {

        if (isSelected) {

            [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;

        }else {

            [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;

        }

    }


        NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
        subMenus.selected = selectedIndex;

        [subMenus save];
        [[CoreDataManager sharedInstance] saveMasterContext];



}

}

If maxSelectCount in a section is 1, the user should only have the ability to select one row. How can I accomplish this?

Ps. the Selected attribute is a BOOl value.

Upvotes: 0

Views: 75

Answers (1)

Ed Liss
Ed Liss

Reputation: 546

Well, a quick solution would be in the didselect method get the index of the selected row and set it to a global variable and then call reloaddata on this table view. In cellForRowAtIndexpath method check if current index is the same as the global index you set in didselextrow and set the check mark accordingly.

Upvotes: 1

Related Questions