2016-10-03 2 views
1

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? 

} 
+0

'playerLbl'과 'playerScore'는 어디에 있습니까? 나는 당신의 코드 스 니펫에서 그것을 보지 못했다. – t4nhpt

+0

@ t4nhpt 더 많은 관련 정보로 PlayerCell 코드를 업데이트했습니다. 'playerScore'는 Player 클래스의 속성입니다. PlayerStore는 Player 속성을 관리하는 클래스입니다 – Macness

답변

1

이 경우 대리자 패턴을 사용합니다. Main.swift가 구현하고 PlayerCell.swift가 선택적 속성으로 사용하는 프로토콜을 만듭니다. 그래서 예를 들면 :

protocol PlayerIncrementor { 
    func increment(by: Int) 
    func decrement(by: Int) 
} 

그런 다음이 프로토콜을 PlayerCell.swift의 내부

extension Main: PlayerIncrementor { 
    func increment(by: int) { 
     //not 100% what you wanted to do with the value here, but this is where you would do something - in this case incrementing what was identified as your model 
     PlayerStore.player.playerScore += by 
    } 
} 

을 구현하기 위해 Main.swift에 확장자를 사용, 대리자 속성을 추가하고 @IBAction

에 위임 증가 메소드를 호출 마지막으로
class PlayerCell: UITableViewCell { 

    var delegate: PlayerIncrementor? 

    @IBOutlet weak var increaseBtn: UIButton! 

    @IBAction func increaseBtnPressed(_ sender: AnyObject) { 
     let tag = sender.tag 

     //call the delegate method with the amount you want to increment 
     delegate?.increment(by: tag)  
    } 

는 - 그것은 모든 일을 만드는 PlayerCell UITableViewCell.

에 대리인으로 홈페이지 할당
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerCell", for: indexPath) as? PlayerCell { 

    //self, in this case is Main - which now implements PlayerIncrementor 
    cell.delegate = self 

    //etc 
+0

두 개의 버튼이있는 경우 (하나가 증가하고 다른 하나가 감소 할 때) PlayerCell에 델리게이트를 추가하는 방법은 무엇입니까? – Macness

+0

@Macness - 프로토콜에 다른 방법을 추가 할 수 있습니다 - 예제를 업데이트했습니다 – syllabix

+0

@Macness - PlayerCell에는 두 개의 버튼이 있습니까? – syllabix

관련 문제