Как сделать выделенную ячейку другим цветом для пользовательских ячеек UITableViewCell - PullRequest
0 голосов
/ 11 июля 2020

Я хочу сделать выделенную ячейку определенным цветом c (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()
        
    }
    
}

Я перепробовал почти все ответы на переполнение стека, и у меня ничего не сработало.

1 Ответ

0 голосов
/ 11 июля 2020

Не могли бы вы попробовать реализовать метод didSelectRowAtIndexPath в дополнение к методу cellForRowAtIndexPath? В этом случае вам не понадобится фоновая кнопка, а также вы получите обратный вызов в методе didSelectRowAtIndexPath при нажатии любой строки в табличном представлении.


func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell = tableView.cellForRow(at: indexPath)
    selectCell.backgroundColor = .red //change to any color you want it to change to on selection
    performSegue(withIdentifier: "CommunicationSegue", sender: self)
}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...