为了美化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 p_w_picpathNamed:@"AccessoryButtonNormal.png"] forState:UIControlStateNormal]; 
  4.     [accessoryButton setImage:[UIImage p_w_picpathNamed:@"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; 

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

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

于是乎,好使了!

哎,一个小效果浪费了一天整的时间!