0

I am new to iOS and I am trying to build an app. In the app I wrote a tableview and this table has custom cells containing an image and textfield. User enters names in textfields (like player names), then I need to pass these names to another view controller. I am trying to get these names but I can't. Can anyone help me for this problem? Here is my code:

In tableview controller class:

var playerCount : Int = 0
var playerNumber = 0
var tempPlayers: [Player] = []

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return Int(playerCount)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellForPlayer", for: indexPath) as! PlayerInformationCellTableViewCell

    cell.txtPlayerName.placeholder = "Oyuncu \(playerNumber + 1)"
    playerNumber = playerNumber + 1
    cell.textFieldDidEndEditing(cell.txtPlayerName)
    tempPlayers.append(cell.tmpPlyer)
    return cell
}

Custom cell class:

var playerNames: [Player] = []
var tmpPlyer = Player(name:"",point: 0)

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    txtPlayerName.delegate = self
    usrImgView.image = #imageLiteral(resourceName: "avatar")
    txtPlayerName.backgroundColor = UIColor.flatWhite
    txtPlayerName.layer.cornerRadius = 5
    txtPlayerName.textAlignment = .center
}

func textFieldDidEndEditing(_ textField: UITextField) {
    if(txtPlayerName.text != nil ){
       // var tempPlayer = Player(name: txtPlayerName.text!,point: 0)
        //playerNames.append(tempPlayer)
        tmpPlyer = Player(name: txtPlayerName.text!,point: 0)
    }
}

I am trying to get text with the didEndEditing method but I am open to suggestions.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • You can use a delegation pattern to let the cell pass the text back to the view controller where it can update its data model. Something like this https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 – Paulw11 Jul 06 '18 at 21:55
  • I can't follow this instructions :( I sad I am new in IOS but I will try thanks a lot @Paulw11 :) have a nice day sir – Mertalp Tasdelen Jul 06 '18 at 22:06
  • is textFieldDidEndEditing this function call ? if not you need yo give delegate method. – Hardik Thakkar Jul 07 '18 at 05:52
  • @HardikThakkar thanks for trying to help but I managed the problem with using deleges and protocol – Mertalp Tasdelen Jul 08 '18 at 14:04

1 Answers1

1

You need to use Protocol and delegation pattern. In this pattern, you pass back your data from tableviewcell to view controller.

Have a look at this tutorial

https://medium.com/@aapierce0/swift-using-protocols-to-add-custom-behavior-to-a-uitableviewcell-2c1f09610aa1

Hitesh Agarwal
  • 1,943
  • 17
  • 21
  • Thanks a lot I managed the issue with the great help of this article. any way I have question but I told before I am new in this environment. Thanks all for giving advise have a nice day all ! – Mertalp Tasdelen Jul 08 '18 at 13:40