【发布时间】:2020-07-11 16:02:48
【问题描述】:
我想让选定的单元格成为特定颜色 (UIColor.systemGray)。我已经尝试了几乎所有的答案,但由于某种原因,我的细胞无法正常工作。
这是我的视图控制器类的一部分:
extension CommunicationViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return communicationcells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let communicationCell = communicationcells[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "CommunicationCell") as! CommunicationCell
cell.backgroundButton.tag = indexPath.row
cell.backgroundButton.addTarget(self, action: #selector(buttonTapped(_:)), for: .touchUpInside)
cell.setCommunication(communication: communicationCell)
return cell
}
struct Holder {
static var _myComputedProperty:Int = -1
}
var myComputedProperty:Int {
get {
return Holder._myComputedProperty
}
set(newValue) {
Holder._myComputedProperty = newValue
}
}
@objc func buttonTapped(_ sender: UIButton) {
myComputedProperty = sender.tag
performSegue(withIdentifier: "CommunicationSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailsController = segue.destination as! CommunicationDetailsViewController
detailsController.passthroughstring = String(myComputedProperty)
}
}
这是我的通信单元控制器:
class CommunicationCell: UITableViewCell {
@IBOutlet var backgroundcontentView: UIView!
@IBOutlet weak var CommunicationType: UILabel!
@IBOutlet weak var subject: UILabel!
@IBOutlet weak var partnerType: UILabel!
@IBOutlet weak var partnerID: UILabel!
@IBOutlet weak var effectiveFrom: UILabel!
@IBOutlet weak var effectiveTo: UILabel!
@IBOutlet weak var downtimeFrom: UILabel!
@IBOutlet weak var downtimeTo: UILabel!
@IBOutlet weak var backgroundButton: UIButton!
@IBOutlet var collectionOfCommunicationLabels: Array<UILabel>!
func applyTheme() {
for i in 0..<collectionOfCommunicationLabels.count {
collectionOfCommunicationLabels[i].textColor = Theme.current.textColor
collectionOfCommunicationLabels[i].backgroundColor = Theme.current.backgroundColor
}
backgroundcontentView.backgroundColor = Theme.current.backgroundColor
CommunicationType.textColor = Theme.current.textColor
subject.textColor = Theme.current.textColor
partnerType.textColor = Theme.current.textColor
partnerID.textColor = Theme.current.textColor
effectiveFrom.textColor = Theme.current.textColor
effectiveTo.textColor = Theme.current.textColor
downtimeFrom.textColor = Theme.current.textColor
downtimeTo.textColor = Theme.current.textColor
// backgroundButton.backgroundColor = Theme.current.backgroundColor
}
func setCommunication(communication: Communication) {
CommunicationType.text = communication.communicationType
subject.text = communication.subject
partnerType.text = communication.partnerType
partnerID.text = communication.partnerID
effectiveFrom.text = communication.effectiveFrom
effectiveTo.text = communication.effectiveTo
downtimeFrom.text = communication.downtimeFrom
downtimeTo.text = communication.downtimeTo
applyTheme()
}
}
我已经尝试了几乎所有关于堆栈溢出的答案,但对我没有任何帮助。
【问题讨论】:
-
使用 tableView 委托 didSelectRowAt indexPath 而不是使用按钮作为背景视图。