-2

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;
}

AndUITableViewdidSelectRowAtIndexPath方法如下

#pragma mark - Table view delegate

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


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

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

}

这里,checkboxSelections是一个NSUIntegercheckboxSelections将所有选定的行累积为该整数的单个位。我的问题是如何将选定的行作为数组或一组行号获取?

4

2 回答 2

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

 }
于 2013-07-10T09:29:27.760 回答
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;
}

希望对你有帮助。。

于 2013-07-10T09:30:00.590 回答