В MainVC.swift
Я занимаюсь тегом моего обычая «PlayerCell
». Я хочу, чтобы нажать increaseBtn
(UIButton
), который будет увеличивать playerLbl.text
(UILabel
) один, но и обновить моя модель (PlayerStore.player.playerScore: Int)
Обновить модель через UIButton в UITableViewCell
Main.swift:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerCell", for: indexPath) as? PlayerCell {
let player = players[indexPath.row]
cell.updateUI(player: player)
cell.increaseBtn.tag = indexPath.row
cell.decreaseBtn.tag = indexPath.row
return cell
} else {
return UITableViewCell()
}
}
PlayerCell.swift
класс PlayerCell: UITableViewCell {
@IBOutlet weak var playerLbl: UILabel!
@IBOutlet weak var increaseBtn: UIButton!
@IBOutlet weak var decreaseBtn: UIButton!
@IBOutlet weak var scoreLbl: UILabel!
@IBOutlet weak var cellContentView: UIView!
func updateUI(player: Player){
playerLbl.text = player.playerName
scoreLbl.text = "\(player.playerScore)"
cellContentView.backgroundColor = player.playerColor.color
}
@IBAction func increaseBtnPressed(_ sender: AnyObject) {
let tag = sender.tag
// TODO: send this tag back to MainVC?
}
Где ваши 'playerLbl' и' playerScore'? Я не вижу его в фрагменте кода. – t4nhpt
@ t4nhpt Я обновил код PlayerCell с большей информацией. 'playerScore' является свойством класса Player, PlayerStore - это класс, который управляет атрибутами Player – Macness