博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITbaleView上按钮的单选
阅读量:6993 次
发布时间:2019-06-27

本文共 1880 字,大约阅读时间需要 6 分钟。

设置Id属性,标记是哪个cell

@property (nonatomic,assign)NSInteger Id;

设置一个普通状态和选中状态图片不同的按钮

_choose = [[UIButton alloc]init];        [_choose setImage:[UIImage imageNamed:@"about_未勾选"] forState:UIControlStateNormal];        [_choose setImage:[UIImage imageNamed:@"about_勾选"] forState:UIControlStateSelected];        [_choose addTarget:self action:@selector(choosePressed:) forControlEvents:UIControlEventTouchUpInside];        [self.contentView addSubview:_choose];
- (void)layoutSubviews {
_choose.frame = CGRectMake(10, 120+wei_tiao, 20, 20);}

设置一个可调整选中或者为选中的方法

- (void)setChecked:(BOOL)checked {    if (checked) {        _choose.selected = YES;    }else {        _choose.selected = NO;    }}

cell上按钮的单击事件

/** *  选择勾选 * *  @param sender 按钮 */- (void)choosePressed:(UIButton *)sender {    sender.selected = YES;    if (_chooseBlock) {        _chooseBlock(self.Id);    }}

设置一个全局变量来判定点了哪一个cell

NSInteger _cellIndex;

 

tableView代理:- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    ShippingAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID"];    cell.selectionStyle = UITableViewCellSelectionStyleNone;    cell.Id = indexPath.row;    if (_cellIndex == indexPath.row) {        [cell setChecked:YES];    }else {        [cell setChecked:NO];    }    [cell setChooseBlock:^(NSInteger index) {        _cellIndex = index;        [_tableView reloadData];    }];    return cell;}

效果图:

 

补:使用了block作为属性回调

1.申明block属性

@property (copy, nonatomic) void(^chooseBlock)(NSInteger index);

2.在本类里面调用

if (_chooseBlock) {        _chooseBlock(self.Id);    }

3.使用block属性:

[cell setChooseBlock:^(NSInteger index) {        _cellIndex = index;        [_tableView reloadData];    }];

 

转载于:https://www.cnblogs.com/hxwj/p/4533074.html

你可能感兴趣的文章
linux 命令与文件的查询
查看>>
MYSQL数据库引擎 MYISAM和 INNODB区别
查看>>
设计模式之原型模式
查看>>
BootStrap常用组件及响应式开发
查看>>
TS学习之for..of
查看>>
OpenGL是什么?
查看>>
Oracle - 数据库巡检脚本
查看>>
提高系统性能:从数据访问层开始
查看>>
【转】IOS开发小技巧
查看>>
ECMAScript 类型转换
查看>>
Java的垃圾回收机制
查看>>
SQL Server 2005 的各种版本所支持的功能
查看>>
Java面向对象之多态
查看>>
第一次接触HBuild
查看>>
逆推 Gym 101102J
查看>>
CF 675 div2C 数学 让环所有值变为0的最少操作数
查看>>
P1379 八数码naive题,STL的胜利
查看>>
用户权限模块之oauth2.0
查看>>
几何服务,cut功能测试
查看>>
冲刺第一天
查看>>