MainVC.swift
私はカスタム "PlayerCell
"のタグをキャプチャしています。UITableViewCell内のUIButtonでモデルを更新
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
:私は1つでplayerLbl.text
(
UILabel
)をインクリメントするだけでなく、私のモデル
(PlayerStore.player.playerScore: Int)
Main.swiftを更新しますincreaseBtn
(UIButton
)を押します
クラス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