如何代码实现 tableview 全部勾选检查框
时间: 2023-08-18 15:08:43 浏览: 55
以下是一个简单的示例代码,演示如何实现 tableview 中的全部勾选检查框。
在ViewController.h文件中,添加以下属性和方法:
```objective-c
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *selectedArray;
@property (nonatomic, assign) BOOL isAllSelected;
- (void)selectAllRows:(id)sender;
```
在ViewController.m文件中,实现以下代码:
```objective-c
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化tableview
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
// 初始化selectedArray
self.selectedArray = [NSMutableArray array];
for (NSInteger i = 0; i < 10; i++) {
[self.selectedArray addObject:@(NO)];
}
// 添加“全选”按钮
UIBarButtonItem *selectAllButton = [[UIBarButtonItem alloc] initWithTitle:@"全选" style:UIBarButtonItemStylePlain target:self action:@selector(selectAllRows:)];
self.navigationItem.rightBarButtonItem = selectAllButton;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
// 添加勾选框
if (![cell.contentView viewWithTag:100]) {
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
selectButton.tag = 100;
selectButton.frame = CGRectMake(10, 10, 30, 30);
[selectButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
[selectButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateSelected];
[selectButton addTarget:self action:@selector(selectRow:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:selectButton];
}
// 设置勾选框状态
UIButton *selectButton = [cell.contentView viewWithTag:100];
selectButton.selected = [self.selectedArray[indexPath.row] boolValue];
// 设置cell内容
cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row+1];
return cell;
}
- (void)selectRow:(UIButton *)sender {
// 获取点击的cell的indexPath
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
// 修改勾选状态
BOOL isSelected = [self.selectedArray[indexPath.row] boolValue];
isSelected = !isSelected;
[self.selectedArray replaceObjectAtIndex:indexPath.row withObject:@(isSelected)];
// 更新勾选框状态
sender.selected = isSelected;
// 判断是否全部勾选,更新“全选”按钮状态
NSInteger selectedCount = 0;
for (NSNumber *isSelected in self.selectedArray) {
if ([isSelected boolValue]) {
selectedCount++;
}
}
self.isAllSelected = (selectedCount == self.selectedArray.count);
self.navigationItem.rightBarButtonItem.title = self.isAllSelected ? @"取消全选" : @"全选";
}
- (void)selectAllRows:(id)sender {
// 修改勾选状态
self.isAllSelected = !self.isAllSelected;
for (NSInteger i = 0; i < self.selectedArray.count; i++) {
[self.selectedArray replaceObjectAtIndex:i withObject:@(self.isAllSelected)];
}
// 更新勾选框状态
[self.tableView reloadData];
// 更新“全选”按钮状态
self.navigationItem.rightBarButtonItem.title = self.isAllSelected ? @"取消全选" : @"全选";
}
- (void)finishButtonClicked:(id)sender {
// 遍历勾选状态进行操作
for (NSInteger i = 0; i < self.selectedArray.count; i++) {
if ([self.selectedArray[i] boolValue]) {
// 执行相应的操作
}
}
}
```
以上代码实现了在 tableview 中添加全部勾选检查框的功能,通过点击勾选框来修改勾选状态,通过 “全选” 按钮来修改所有行的勾选状态。在点击完成按钮时,遍历所有行的勾选状态进行相应操作。