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
轉載于:https://www.cnblogs.com/pengyingh/articles/2339188.html
總結
以上是生活随笔為你收集整理的UITableView定制accessoryView出现的连带问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: servlet与MySQL商品管理系统_
- 下一篇: pdf嵌入字体(不用adobe pdf打