2016-05-16(UITableView上面的单元格里面的选择按钮)

[selectButton setImage:[UIImage imageNamed:@"003"] forState:UIControlStateNormal];//非选中状态时按钮的图片

[selectButton setImage:[UIImage imageNamed:@"004"] forState:UIControlStateSelected];//选中状态时按钮的图片

[selectButton addTarget:self action:@selector(selectButtonAction:) forControlEvents:UIControlEventTouchUpInside];//给按钮添加点击动作,在点击时进行的操作当中进行点击状态的更换以及记录当前的点击状态

cell.accessoryView = selectButton;//如果选择通用的那种方式(加载到contentView上面),那么通过(int)[_tableView indexPathForCell:cell].row获取到的row就一直都是0.所以我们一定要采用当前的这种方式添加按钮。

//实现selectButtonAction   

- (void)selectButtonAction:(id)sender{

UIButton *button = (UIButton *)sender;

button.selected = !button.selected;

//下面的代码作用是:记录当前单元格中按钮的选中状态

UITableViewCell *cell = (UITableViewCell *)[button superview];

int row = (int)[_tableView indexPathForCell:cell].row;

if (row == 0) {

_isSelectedA = button.selected;}

if (row == 1) {

_isSelectedB = button.selected;}

if (row == 2) {

_isSelectedC = button.selected;}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容