我创建了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);
}
是checkboxSelections
。 NSUInteger
将所有选定的行累积为该整数的单个位。我的问题是如何将选定的行作为一个数组或一组行号?
答案 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;
}
希望它会帮助你......