UITableView Cell上的复选框

时间:2013-07-10 08:43:24

标签: iphone ios objective-c

我创建了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;
}

UITableView的{​​{1}}方法如下

didSelectRowAtIndexPath

此处#pragma mark - Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { checkboxSelections ^= (1 << indexPath.row); [self.tblViewExtra reloadData]; NSLog(@"CheckBox selections %d", checkboxSelections); } checkboxSelectionsNSUInteger将所有选定的行累积为该整数的单个位。我的问题是如何将选定的行作为一个数组或一组行号?

2 个答案:

答案 0 :(得分: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];

 }

答案 1 :(得分: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;
}

希望它会帮助你......