本文共 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
- (void)setSelected:(BOOL)selected animated:(BOOL)animated UITableViewCell (手机离开cell,并选中cell时) selected:YES
-------------------------------------------------------------------------------------------------------------------
其实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]; }}
转载地址:http://pnosi.baihongyu.com/