2014-02-27 23 views
0

我需要在用checkMark選擇的部分中只有一行。我使用NSFetchresults控制器從核心數據存儲中檢索數據。這裏我有一個擁有選定標誌的實體。目前我在我的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,用戶應該只需要選擇一個行的能力。我怎樣才能做到這一點?

Ps。 Selected屬性是BOOl值。

回答

1

好吧,快速解決方案將在didselect方法獲取所選行的索引並將其設置爲全局變量,然後在此表視圖上調用reloaddata。在cellForRowAtIndexpath方法中,檢查當前索引是否與您在didselextrow中設置的全局索引相同,並相應地設置複選標記。