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
: 나는(PlayerStore.player.playerScore: Int)
Main.swift 하나에 의해 playerLbl.text
(UILabel
) 증가뿐만 아니라 내 모델 업데이트됩니다되는 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