日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UITableViewCell高亮时其子视图的状态修改

發布時間:2024/4/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UITableViewCell高亮时其子视图的状态修改 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為了進行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

tableView:willSelectRowAtIndexPath:           ?UITableView (手機離開cell,并選中cell時)

- (void)setSelected:(BOOL)selected?animated:(BOOL)animated     ?UITableViewCell (手機離開cell,并選中cell時) selected:YES

tableView:didSelectRowAtIndexPath:            ?UITableView?(手機離開cell,并選中cell時)

-------------------------------------------------------------------------------------------------------------------

其實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];
}
}

至此,在cell高亮的情況下,其accessoryView都不會受其影響,變為高亮狀態了,同樣的道理都適用于cell里的任一子視圖





轉載于:https://www.cnblogs.com/Piosa/archive/2012/02/15/2352143.html

總結

以上是生活随笔為你收集整理的UITableViewCell高亮时其子视图的状态修改的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。