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