@property (assign, nonatomic) NSIndexPath *selIndex; //单选选中的行
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//取消之前的选择
UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
celled.accessoryType = UITableViewCellAccessoryNone;
//记录当前的选择的位置
_selIndex = indexPath;
//当前选择的打钩
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%zi组,第%zi行",indexPath.section+1,indexPath.row];
if (_selIndex == indexPath) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
@property (strong, nonatomic) NSMutableArray *selectIndexs; //多选选中的行
@property (nonatomic, assign) BOOL isSingle; //单选还是多选
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"多选" style:UIBarButtonItemStylePlain target:self action:@selector(singleSelect)]; self.navigationItem.rightBarButtonItem = rightItem; //初始化多选数组 _selectIndexs = [NSMutableArray new];
//单选还是多选按钮点击事件
-(void)singleSelect{
_isSingle = !_isSingle;
if (_isSingle) {
self.navigationItem.rightBarButtonItem.title = @"多选";
self.title = @"(单选)";
//切换为单选的时候,清除多选数组,重新加载列表
[self.selectIndexs removeAllObjects];
[self.tableView reloadData];
}else{
self.title = @"(多选)";
self.navigationItem.rightBarButtonItem.title = @"单选";
}
}
//选中某一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (_isSingle) { //单选
//取消之前的选择
UITableViewCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
celled.accessoryType = UITableViewCellAccessoryNone;
//记录当前的选择的位置
_selIndex = indexPath;
//当前选择的打钩
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{ //多选
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryCheckmark) { //如果为选中状态
cell.accessoryType = UITableViewCellAccessoryNone; //切换为未选中
[_selectIndexs removeObject:indexPath]; //数据移除
}else { //未选中
cell.accessoryType = UITableViewCellAccessoryCheckmark; //切换为选中
[_selectIndexs addObject:indexPath]; //添加索引数据到数组
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellid = @"cellid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = [NSString stringWithFormat:@"第%zi组,第%zi行",indexPath.section+1,indexPath.row];
if (_isSingle) { //单选
if (_selIndex == indexPath) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}else{ //多选
cell.accessoryType = UIAccessibilityTraitNone;
for (NSIndexPath *index in _selectIndexs) {
if (indexPath == index) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}
}
return cell;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有