MainVC.swift
我正在捕获自定义“PlayerCell
”的标记。我想按increaseBtn
(UIButton
),这将增加了playerLbl.text
(UILabel
)由一个也更新我的模型(PlayerStore.player.playerScore: Int)
通过UITableViewCell中的UIButton更新模型
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
class 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是一个管理播放器属性的类 – Macness