仅基于Core数据对象选择带有复选标记的一行

时间:2014-02-27 17:11:10

标签: ios iphone objective-c core-data

我需要在使用checkMark选择的部分中只有一行。我使用NSFetchresultscontroller从核心数据存储中检索数据。在这里,我有一个具有选定标志的实体。目前,我在didSelectRowAtIndexpath方法中有以下代码。

-(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];



}

}

如果某个部分中的maxSelectCount为1,则该用户应该只能选择一行。我怎么能做到这一点?

聚苯乙烯。 Selected属性是BOOl值。

1 个答案:

答案 0 :(得分:1)

嗯,快速解决方案是在didselect方法中获取所选行的索引并将其设置为全局变量,然后在此表视图上调用reloaddata。在cellForRowAtIndexpath方法中,检查当前索引是否与您在didselextrow中设置的全局索引相同,并相应地设置复选标记。