UITableView定制accessoryView出现的连带问题
為了美化UI,想實(shí)現(xiàn)如下圖的效果:點(diǎn)擊高亮
出發(fā)點(diǎn)是好的。沒想到,出現(xiàn)了下圖的連帶問題:選擇一行的時(shí)候,竟然連帶的出現(xiàn)了高亮效果
這個(gè)如何是好?經(jīng)過網(wǎng)絡(luò)搜索,發(fā)現(xiàn)我不是第一個(gè)遇到這樣的問題: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;
連使用方法都是一樣的,看來我不是第一個(gè)這么干的。這哥哥也不給解決辦法,不知道最后搞定沒問題。困惑了一天以后,終于讓我找到了一條小縫隙,實(shí)現(xiàn)了下圖的效果:
這個(gè)點(diǎn)擊行的時(shí)候,整行高亮是系統(tǒng)自帶的功能,沒辦法改變,怎么辦呢?釜底抽薪,在它高亮完以后再把效果取消!這個(gè)出發(fā)點(diǎn)是對(duì)的,可是浪費(fèi)了大把的時(shí)間以后發(fā)現(xiàn)還是達(dá)不到預(yù)期的效果,怎么呢?查sdk的時(shí)候無意間發(fā)現(xiàn)UITablview有個(gè)willSelectRowAtIndexPath的方法吧。好吧,這個(gè)willSelectRowAtIndexPath比didSelectRowAtIndexPath應(yīng)該靠前吧,在這里面試一下
1 - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{2 UITableViewCell *Cell = [tableView cellForRowAtIndexPath:indexPath];
3 [(UIButton *)Cell.accessoryView setHighlighted:NO];
4 return indexPath;
5 }
?
開始直接用的上面的代碼,發(fā)現(xiàn)好使靠人品,后來想想即然在這個(gè)地方可以,那就延時(shí)執(zhí)行一下,于是用了:
- (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;
}
于是乎,好使了!
轉(zhuǎn)載自:http://rainbird.blog.51cto.com/211214/687170
轉(zhuǎn)載于:https://www.cnblogs.com/pengyingh/articles/2339188.html
總結(jié)
以上是生活随笔為你收集整理的UITableView定制accessoryView出现的连带问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: servlet与MySQL商品管理系统_
- 下一篇: pdf嵌入字体(不用adobe pdf打