博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UITableViewCell高亮时其子视图的状态修改
阅读量:4109 次
发布时间:2019-05-25

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

为了进行UI自定义,修改了UITableViewCell的accessoryView,如下

UIButton * accessoryDetailDisclosureButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];[accessoryDetailDisclosureButton setImage:[UIImage imageNamed:@"accessoryDetailDisclosureButton_normal.png"] forState:UIControlStateNormal];[accessoryDetailDisclosureButton setImage:[UIImage imageNamed:@"accessoryDetailDisclosureButton_highlighted.png"] forState:UIControlStateHighlighted];[accessoryDetailDisclosureButton addTarget:self action:@selector(accessoryDetailDisclosureButtonPress:) forControlEvents:UIControlEventTouchUpInside];cell.accessoryView = accessoryDetailDisclosureButton;[accessoryDetailDisclosureButton release];

实现如下的效果

可是在cell被选中的时候,却连带把accessoryView的状态也修改成了highlighted,如下

最后的解决办法就是继承UITableViewCell来进行cell按下动作时的状态修改,如下

@interface UCaiTableViewCell : UITableViewCell@end@implementation UCaiTableViewCell- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [super touchesBegan:touches withEvent:event];    [(UIButton *)self.accessoryView setHighlighted:NO];}@end

从而实现了如下效果

 

以上的修改办法,虽然可以让cell在按下的时候,accessoryView的highlighted修改为NO,但是在手指向上离开cell时,accessoryView的highlighted任然被修改成了YES;

所以要想实现cell被按下和松开时都不会影响其上子视图的highlighted的话,需要了解TableView与TableViewCell在按下时的协作关系。

当手指在cell上按下,cell被选中的这一动作中,系统调用了如下一序列方法

-------------------------------------------------------------------------------------------------------------------

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated  UITableViewCell (手指按下cell时)      highlighted:YES

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated  UITableViewCell (手机离开cell时)      highlighted:NO

tableView:willSelectRowAtIndexPath:            UITableView (手机离开cell,并选中cell时)

- (void)setSelected:(BOOL)selected animated:(BOOL)animated      UITableViewCell (手机离开cell,并选中cell时) selected:YES

tableView:didSelectRowAtIndexPath:             UITableView (手机离开cell,并选中cell时)

-------------------------------------------------------------------------------------------------------------------

其实cell上的子视图在cell被高亮的同时也会被高亮,是因为UITableViewCell的selectedBackgroundView影响。当UITableViewCell为选中状态时,UITableViewCell把selectedBackgroundView当作一个子视图来添加,selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者所有其它视图之下。当调用setSelected: animated:这一方法时,会导致selectedBackgroundView以一个alpha消化的状态来出现和消失。

因此我们可知道,如果UITableViewCell的selectionStyle值为UITableViewCellSelectionStyleNone时,selectedBackgroundView将不起作用。

我们进行以下的子类化,就可解决以上我们遇到的问题

@interface UCaiTableViewCell : UITableViewCell@end@implementation UCaiTableViewCell@synthesize piosaDelegate = _piosaDelegate;- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{    [super setHighlighted:highlighted animated:animated];        if (highlighted) {        [(UIButton *)self.accessoryView setHighlighted:NO];    }}- (void)setSelected:(BOOL)selected animated:(BOOL)animated{    [super setSelected:selected animated:animated];        if (selected) {        [(UIButton *)self.accessoryView setHighlighted:NO];    }}
至此,在cell高亮的情况下,其accessoryView都不会受其影响,变为高亮状态了,同样的道理都适用于cell里的任一子视图

转载地址:http://pnosi.baihongyu.com/

你可能感兴趣的文章
react 解决警告Warning: Each child in a list should have a unique “key” prop.
查看>>
安装react初始化项目报错 :无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\create-react-app.ps1,因为在此系统上禁止运行
查看>>
babel-plugin-import配置babel按需引入antd模块
查看>>
浏览器对ES6新语法支持兼容处理
查看>>
ajax请求中自定义添加请求头token两种方式
查看>>
前端常用加密处理vue使用md5加密
查看>>
vue 前端常用加密处理 md5 加密
查看>>
Vue前端常用加密处理base64加密解密
查看>>
VUE前端常用加密处理des加密和解密
查看>>
nodemon代替node自动重启项目
查看>>
react 解决重命名生命周期componentWillMount
查看>>
vs code编写react代码 jsx中html标签Tab键自动补全
查看>>
vue.js限制输入框只能输入整数不含小数点
查看>>
字符串数组初始化转化
查看>>
表单提交enter触发提交
查看>>
解决bootstrapTable中的width值设置无效的问题方法
查看>>
bootstrap table th内容太多表格撑破(自动换行)
查看>>
layui快速使用
查看>>
layui引用layui.css,layui.js后为什么表单不显示,不渲染?
查看>>
layui提交表单复选框CheckBox点击第一次无效解决方案
查看>>