當單元格在故事板中創建並與IBOutlet連接時,當單元格被選中圖像視圖的背景更改爲UILabel和UIImageView時,我有一個自定義UITableview單元格選擇顏色?我怎樣才能讓UIImageView的背景顏色保持不變?當單元格被選中時,UIImageView消失但不是UILabel
感謝
當單元格在故事板中創建並與IBOutlet連接時,當單元格被選中圖像視圖的背景更改爲UILabel和UIImageView時,我有一個自定義UITableview單元格選擇顏色?我怎樣才能讓UIImageView的背景顏色保持不變?當單元格被選中時,UIImageView消失但不是UILabel
感謝
嘗試電話:
cell.selectionStyle = UITableViewCellSelectionStyleNone
在
cellForRowAtIndexPath
。
和(可選)重寫:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
迅速:
override func setSelected(selected: Bool, animated: Bool)
斯威夫特用法示例:
override func setSelected(selected: Bool, animated: Bool) {
if selected {
yourView.alpha = 0.5
} else {
yourView.alpha = 1.0
}
}
你也可以改變內部animateWithDuration
擋住你的視線的阿爾法:
override func setSelected(selected: Bool, animated: Bool) {
UIView.animateWithDuration(0.3, animations: {
if selected {
yourView.alpha = 0.5
} else {
yourView.alpha = 1.0
}
})
}
對象 -用法示例:
#import "YourTableViewCell.h"
@implementation YourTableViewCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
//your code
// example: yourView.backgroundColor = [UIColor greenColor];
}
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:YES];
//your code
// example: yourView.backgroundColor = [UIColor greenColor];
}
@end
您可以更改單元格的ImageView在你UITableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell from the selected row.
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
//change custom cell's imageView to desired backgroundColor
selectedCell.imageView = [UIColor clearColor];
}
-didSelectRowAtIndexPath
而且不要忘記實現UITableViewDelegate
和UITableViewDatasource
我想可能是發生由於tableviewcell選擇。 只需使用下面的代碼片斷
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableview deselectRowAtIndexPath:indexPath animated:YES];
}
這可以幫助你。如果沒有,那麼請分享您的實施信息。
你可能已經設置圖像視圖的背景色清除顏色。
如何以及在哪裏呼叫setSelected? –
正如你所看到的,在函數聲明之前有'override'關鍵字。你不必調用它。 UITableView會爲你調用它。只要將這個方法放在你的UITableViewCell子類中。 – zuziaka
我使用objc代碼沒有覆蓋 –