[英]Multiple Selection In TableView Swift
當我嘗試編輯和選擇 Tableview 單元格時,它正在選擇單元格但問題是在我的單元格中有一個視圖可用。 因此,當它突出顯示單元格時,紅色視圖將從單元格中消失。
那么我將如何在選擇 tableView 后顯示紅色顏色視圖
此代碼的鏈接是https://github.com/anupgupta-arg/iOS-Swift-tableViewSelect
現在我的細胞設計是
我建議使用帶有純紅色圖像的 UIImageView而不是UIView 。 請參閱我的示例代碼。 我沒有做太多代碼,一切都是通過故事板實現的。 請檢查鏈接的圖像以了解解決方案。
Prototype Cell它有一個標簽,UIImage 和 UIView,背景顏色為紅色。 我設置為一些純色圖像的 UIImage。
有了這個,我的表格視圖看起來像:沒有螞蟻選擇的 TableView
【TableView with selection】可以看到UIImageView沒有受到cell選擇的影響,UIView受到影響。 3
在 SimpleTableViewCell 中使用此方法:
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
centerView.backgroundColor = .red
}
當在 tableview 中選擇一個單元格時,它會將其所有子視圖的背景顏色更改為清除顏色,這就是每次更改紅色的原因。
這只是在每次發生時重置它的顏色。
如果您有多個視圖,請改用它:
override func setSelected(_ selected: Bool, animated: Bool) {
contentView.backgroundColor = .gray
}
這不會調用 super,因此不會出現問題。
將multipleSelectedBackgroundView用於選定狀態,並將您的另一個視圖(紅色)放在 contentView 中。
如果您只想更改單元格的選定顏色,那么試試這個
目標-C:
UIView *bgColorView = [[UIView alloc] init];
bgColorView.backgroundColor = [UIColor redColor];
[cell setSelectedBackgroundView:bgColorView];
迅速:
let bgColorView = UIView()
bgColorView.backgroundColor = UIColor.red
cell.selectedBackgroundView = bgColorView
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.