![](/img/trans.png)
[英]Highlight long pressed table view cell and unhighlight in iOS using objective-C
[英]Highlighting table view cell will not highlight subviews of table view cell using objective-C
我有帶有自定義單元格的表格視圖。 在表格視圖單元格的每一行中,都有帶有漸變顏色的子視圖,並且每行的背景顏色與子視圖的顏色不同。
當我點擊或觸摸每個表格視圖單元格時,它只是突出顯示表格視圖單元格的背景,而不突出顯示子視圖。 所有單元格內容均未用綠色突出顯示。
我嘗試刪除子視圖的漸變層並向其添加背景色,然后所有單元格內容均突出顯示,但漸變層未突出顯示。
我還需要保存狀態,無論是否突出顯示,如何實現?
這是屏幕截圖,
在cellForRowAtIndexPath方法中
UIView *selection_color = [[UIView alloc] init];
selection_color.backgroundColor = [UIColor greenColor];
cell.selectedBackgroundView = selection_color;
UIView *view = [UIView alloc] init];
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
view.layer.borderColor = [[UIColor colorWithRed:197/255.0 green:197.0/255.0 blue:197.0/255.0 alpha:1.0] CGColor];
view.layer.borderWidth = 1.0f;
view.tag = 10;
[cell.contentView addSubview: view];
NSArray *gradient_colors = [NSArray arrayWithObjects:(id)[UIColor redColor].CGColor, (id)[UIColor whiteColor].CGColor, nil];
NSArray *gradient_locations = [NSArray arrayWithObjects:[NSNumber numberWithInt:0.0],[NSNumber numberWithInt:1.0], nil];
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = gradient_colors;
gradientLayer.locations = gradient_locations;
gradientLayer.masksToBounds = YES;
gradientLayer.frame = view.layer.bounds;
[view.layer addSublayer:gradientLayer];
指導我解決這個問題。
您必須在自定義表格視圖單元格中覆蓋setHighlighted方法,並在子視圖上同時設置突出顯示。
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
self.customView.backgroundColor = [UIColor redColor];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.