2013-07-10 101 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]; 

}