简体   繁体   中英

Updating Label in Cell

I have a TableView which rows contain label and two buttons. What I wanna do is that when a user clicks the first button "Set Name", a pop up view comes up in which he can input text from keyboard. After hitting "Set", pop up view is dismissed and label inside a row containing the clicked button changes to the input text. I set the delegates but I cannot make label to change.

TableView:

import UIKit

class SetGame: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    var numOfPlayers = Int()

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
        cell.Name.text = "Player \(indexPath.row + 1)"
        cell.btn1.tag = indexPath.row
        cell.btn2.tag = indexPath.row

        return cell
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func setName(sender: UIButton)
    {   
        let thisVC = storyboard?.instantiateViewController(withIdentifier: "SetName") as! SetName
        thisVC.delegate = self
        present(thisVC, animated: true, completion: nil)
    }

    @IBAction func setFingerprint(_ sender: UIButton)
    {

    }

    @IBAction func unwindToSetGame(_ segue: UIStoryboardSegue)
    {
        print("unwinded to SetGame")
    }

    @IBOutlet weak var tableView: UITableView!
}

extension SetGame: nameDelegate
{
    func named(name: String)
    {
        let indexP = IndexPath(row: 0, section: 0)
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell
        cell.Name.text = "bkjhvghcjhkv"
        //wanted to see if it changes first cell. But doesn't work

    }
}

TableViewCell Class:

import UIKit

class TableViewCell: UITableViewCell
{

    override func awakeFromNib()
    {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }

    @IBOutlet weak var Name: UILabel!
    @IBOutlet weak var btn1: UIButton!
    @IBOutlet weak var btn2: UIButton!

}

Pop up View:

import UIKit

protocol nameDelegate
{
    func named(name: String)
}

class SetName: UIViewController
{
    var delegate: nameDelegate!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        window.layer.borderWidth = 1
        window.layer.borderColor = UIColor.white.cgColor
    }

    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
    }

    @IBAction func closePopUp(_ sender: Any)
    {
        if input.text != ""
        {
            delegate.named(name: input.text!)
        }
        dismiss(animated: true, completion: nil)
    }

    @IBOutlet weak var input: UITextField!
    @IBOutlet weak var window: UIView!
}

Replace this

let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexP) as! TableViewCell

with

let cell = tableView.cellForRow(at:indexP) as! TableViewCell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM