首页 > 解决方案 > 更新单元格中的标签

问题描述

我有一个 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 类:

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!

}

弹出视图:

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!
}

标签: iosswiftuitableview

解决方案


替换这个

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

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

推荐阅读