#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *detail;
@property(nonatomic,assign,getter=isSelected)BOOL select;
@end
#import "TableViewController.h"
#import "Person.h"
@interface TableViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *arrayM;
@property(strong,nonatomic)NSMutableArray *selectRows;
@end
@implementation TableViewController
/**
* 懒加载
*
* @return 返回需要显示的数据
*/
- (NSMutableArray *)arrayM{
if (_arrayM==nil) {
_arrayM = [NSMutableArray array];
for (int i=0; i<20; i++) {//填充假数据
Person *person = [[Person alloc]init];
person.name= [NSString stringWithFormat:@"%dzc",i];
person.detail = [NSString stringWithFormat:@"%ddetail",i];
person.select = NO;
[_arrayM addObject:person];
}
}
return _arrayM;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
_selectRows = [NSMutableArray array]; //
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
headerView.backgroundColor = [UIColor redColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(headerView.frame.size.width-200, 20, 200, 30);
[btn setTitle:@"提交复选结果" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(commitAction) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:btn];
[self.view addSubview:headerView];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, headerView.frame.size.height, self.view.frame.size.width, self.view.frame.size.height-headerView.frame.size.height) style:UITableViewStylePlain];
self.tableView.dataSource =self;
self.tableView.delegate =self;
[self.view addSubview:self.tableView];
}
#pragma mark --UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSLog(@"%zi",section);
return [self.arrayM count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID =@"cell";
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
Person *person = self.arrayM[indexPath.row];
cell.textLabel.text = person.name;
cell.detailTextLabel.text = person.detail;
if (person.select) {
cell.accessoryType =UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Person *person = self.arrayM[indexPath.row];
if (person.isSelected) {
person.select = NO;
[_selectRows removeObject:@(indexPath.row)];
}else{
person.select = YES;
[_selectRows addObject:@(indexPath.row)];
}
[tableView reloadRowsAtIndexPaths:@ [indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark --commitAction
- (void)commitAction{
NSMutableString *strs = [[NSMutableString alloc]initWithString:@"你选中的行数"];
[_selectRows enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
Person *person = self.arrayM[idx];
NSLog(@"%@",person.name);
[strs appendFormat:@"%@,",person.name];
}];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:strs preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
}