Fork me on GitHub

UITableView定制accessoryView出现的连带问题

为了美化UI,想实现如下图的效果:点击高亮

出发点是好的。没想到,出现了下图的连带问题:选择一行的时候,竟然连带的出现了高亮效果

这个如何是好?经过网络搜索,发现我不是第一个遇到这样的问题:custom-accessory-button-highlight-triggered-by-didselectrowatindexpath

1 UIButton *accessoryButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
2 accessoryButton.frame = CGRectMake(0, 0, 32, 32);
3 [accessoryButton setImage:[UIImage imageNamed:@"AccessoryButtonNormal.png"] forState:UIControlStateNormal];
4 [accessoryButton setImage:[UIImage imageNamed:@"AccessoryButtonInverse.png"] forState:UIControlStateHighlighted];
5 [accessoryButton addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
6 cell.accessoryView = accessoryButton;



连使用方法都是一样的,看来我不是第一个这么干的。这哥哥也不给解决办法,不知道最后搞定没问题。困惑了一天以后,终于让我找到了一条小缝隙,实现了下图的效果:

这个点击行的时候,整行高亮是系统自带的功能,没办法改变,怎么办呢?釜底抽薪,在它高亮完以后再把效果取消!这个出发点是对的,可是浪费了大把的时间以后发现还是达不到预期的效果,怎么呢?查sdk的时候无意间发现UITablview有个willSelectRowAtIndexPath的方法吧。好吧,这个willSelectRowAtIndexPath比didSelectRowAtIndexPath应该靠前吧,在这里面试一下

1 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{ 
2 UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
3 [(UIButton *)Cell.accessoryView setHighlighted:NO];
4 return indexPath;
5 }


 

开始直接用的上面的代码,发现好使靠人品,后来想想即然在这个地方可以,那就延时执行一下,于是用了:

- (void)mySelectRow:(UIButton *)actionBtn{ 
[actionBtn setHighlighted:NO];
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
[self performSelector:@selector(mySelectRow:) withObject:(UIButton *)Cell.accessoryView afterDelay:0];
return indexPath;
}



于是乎,好使了!

转载自:http://rainbird.blog.51cto.com/211214/687170

posted on 2012-02-05 18:58  pengyingh  阅读(869)  评论(1编辑  收藏  举报

导航