Всем привет. Не могу разобраться с переиспользованием ячейки tableView.
С сервера приходит сообщение, после которого должна удаляться кнопка в tableViewCell.
Вот функция, которая запускается, когда приходит сообщение с сервера:
func contactHasHadCanceled(indexOfCanceledMessage: Int) {
print("Game Maker has canceled the word \(messages[indexOfCanceledMessage].word)")
messages[indexOfCanceledMessage].isAbleToBeInteracted = false
let indexPath = IndexPath(row: indexOfCanceledMessage, section: 0)
tableView.reloadRows(at: [indexPath], with: .automatic)
}
Вот cellForRowAt:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContactCell
cell.delegate = self
cell.configure(message: self.messages[indexPath.row], playerType: self.playerType, index: indexPath.row)
return cell
}
Вот функции настройки ячейки (Находятся в ContactCell):
private var contactCellButton: UIButton!
func configure(message: Message, playerType: PlayerType, index: Int) {
self.playerType = playerType
self.indexRow = index
setUpDefinitionLabel()
print(message.isAbleToBeInteracted)
if message.isAbleToBeInteracted {
setUpButton()
} else {
unSetUpButton()
}
definitionLabel.text = message.definition
}
func setUpButton() {
contactCellButton = UIButton()
contactCellButton.translatesAutoresizingMaskIntoConstraints = false
addSubview(contactCellButton)
contactCellButton.titleLabel?.numberOfLines = 0
contactCellButton.titleLabel?.adjustsFontSizeToFitWidth = true
contactCellButton.titleLabel?.minimumScaleFactor = 0.5
contactCellButton.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
switch playerType {
case .none:
print("player type in contact cell is not defined")
case .some(.player):
contactCellButton.backgroundColor = UIColor.blue
contactCellButton.setTitle("Contact", for: .normal)
case .some(.gameMaker):
contactCellButton.backgroundColor = UIColor.red
contactCellButton.setTitle("Cancel", for: .normal)
}
//Cornstraints
contactCellButton.rightAnchor.constraint(equalTo: rightAnchor, constant: 5).isActive = true
contactCellButton.leftAnchor.constraint(equalTo: definitionLabel.rightAnchor, constant: 5).isActive = true
contactCellButton.topAnchor.constraint(equalTo: topAnchor, constant: 5).isActive = true
contactCellButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -5).isActive = true
}
func unSetUpButton() {
contactCellButton = nil
}
Проблема в том, что при скроллинге удаленные кнопки снова появляются. Также не все кнопки удаляются. Не могу понять, почему идет неправильное переиспользование.