gpt4 book ai didi

ios - 更新单元格中的标签

转载 作者:行者123 更新时间:2023-11-28 14:36:55 25 4
gpt4 key购买 nike

我有一个 TableView,其中的行包含标签和两个按钮。我想做的是,当用户单击第一个按钮“设置名称”时,会出现一个弹出 View ,他可以在其中从键盘输入文本。点击“设置”后,弹出 View 被关闭,包含单击按钮的行内的标签更改为输入文本。我设置了代表,但无法更改标签。

表格 View :

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!

}

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

最佳答案

替换这个

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

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

关于ios - 更新单元格中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824624/

25 4 0