2013-07-10 38 views
-2

我創建了UITableView,每個cell上都有一個複選框。這是我做的UITableView Cell上的複選框

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier "; 
    ExtraCell *eCell = (ExtraCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (eCell == nil) { 
     NSArray *nib; 

     nib = [[NSBundle mainBundle] loadNibNamed:@"ExtraCell" owner:self options:nil]; 

     for (id oneObject in nib) if ([oneObject isKindOfClass:[ExtraCell class]]) 
      eCell = (ExtraCell *)oneObject; 

    } 
    int flag = (1 << indexPath.row); 

    // update row's accessory if it's "turned on" 
    if (checkboxSelections & flag) 
     eCell.accessoryType = UITableViewCellAccessoryCheckmark; 

    eCell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return eCell; 
} 

而且UITableViewdidSelectRowAtIndexPath方法如下

#pragma mark - Table view delegate 

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


    checkboxSelections ^= (1 << indexPath.row); 
    [self.tblViewExtra reloadData]; 

    NSLog(@"CheckBox selections %d", checkboxSelections); 

} 

這裏,checkboxSelectionsNSUIntegercheckboxSelections將所有選定的行累加爲該整數的單個位。我的問題是如何將選定的行作爲一個數組或一組行數?

+0

如果我理解正確QN,你爲什麼不以一個NSMutableArray在'didSelectRowAtIndexPath'添加行? – HRM

+0

@sajaz你在你的數組中存儲什麼?任何類對象? –

+0

checkboxSelections^=(1 << indexPath.row); ??????你能解釋一下嗎? – Rajneesh071

回答

0

不喜歡的是,

.h

@property (nonatomic, retain) NSMutableArray *selectedRows; 

.m

@synthesis selectedRows;

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

    static NSString *CustomCellIdentifier = @"CustomCellIdentifier "; 
    ExtraCell *eCell = (ExtraCell *)[tableView dequeueReusableCellWithIdentifier: CustomCellIdentifier]; 

    if (eCell == nil) { 
     NSArray *nib; 

     nib = [[NSBundle mainBundle] loadNibNamed:@"ExtraCell" owner:self options:nil]; 

     for (id oneObject in nib) if ([oneObject isKindOfClass:[ExtraCell class]]) 
      eCell = (ExtraCell *)oneObject; 

    } 
    int flag = (1 << indexPath.row); 

    // update row's accessory if it's "turned on" 

    NSString *rowString = [NSString stringWithFormat:@"%d",indexPath.row]; 

    if (checkboxSelections & flag) { 
     eCell.accessoryType = UITableViewCellAccessoryCheckmark; 

     if(![self.selectedRows containsObject:rowString]) 
      [self.selectedRows addObject:rowString]; 
    } else { 

     if([self.selectedRows containsObject:rowString]) 
      [self.selectedRows removeObject:rowString]; 
    } 

    eCell.selectionStyle = UITableViewCellSelectionStyleNone; 

    return eCell; 
} 

希望它會幫助你..

+0

謝謝大家的親切幫助。我能解決它 – sajaz

1

你應該記住每個開關都使用自定義數組或集合。

NSMutableArray包含indexPath的值。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BOOL checkBox = [[self.array objectAtIndex:indexPath.row] boolValue]; 
    checkBox = !checkBox; 
    NSNumber *saveCheckBox = [NSNumber numberWithBool:checkBox]; 
    [self.array replaceObjectAtIndex:indexPath.row withObject:saveCheckBox]; 

}