gpt4 book ai didi

ios - 将文本字段值保存到单元格文本标签(名称)[SWIFT]

转载 作者:行者123 更新时间:2023-11-30 12:19:23 25 4
gpt4 key购买 nike

我要做这样的事情/image/jAGsk.png

因此,如果用户输入点 - 它会将点保存到用户名中。怎么做?我使用函数将 textField 粘贴到 tableViewCell 中。这是 tableViewCell 文件中的代码

@IBOutlet weak var inputScore: UITextField!

public func configure(text: Int?, placeholder: String) {
inputScore.text = String(text!)
inputScore.placeholder = placeholder

inputScore.accessibilityValue = String(text!)
inputScore.accessibilityLabel = placeholder
}

这是 VC 文件中的代码

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "InputScore") as! InputScoreTableViewCell
cell.textLabel?.text = usersIn[indexPath.row]
cell.configure(text: 100, placeholder: "Score")
return cell
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return usersIn.count
}

那么如何保存到用户名下呢?

最佳答案

Use DidSelectRowAtIndexPath method to get cell textLable text in textField.

下面的示例代码:

import UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet var btnOK: UIButton!
@IBOutlet var txtValue: UITextField!
@IBOutlet var tblData: UITableView!
let arrResult = NSMutableArray()

override func viewDidLoad() {
super.viewDidLoad()


tblData.dataSource = self
tblData.delegate = self
btnOK.tag = 57775
btnOK.addTarget(self, action: #selector(applyEdit(sender:)), for: .touchUpInside)
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = arrResult[indexPath.row] as? String ?? ""
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
btnOK.tag = indexPath.row
let cell: UITableViewCell = tableView.cellForRow(at: indexPath)!
txtValue.text = cell.textLabel?.text
setTitle()
}

func setTitle() {
if btnOK.tag == 57775 {
btnOK.setTitle("Add", for: .normal)
}else{
btnOK.setTitle("Update", for: .normal)
}
}

func applyEdit(sender: UIButton) {
if sender.tag == 57775 {
arrResult.add(txtValue.text ?? "")
}else{
arrResult.removeObject(at: sender.tag)
arrResult.insert(txtValue.text ?? "", at: sender.tag)
sender.tag = 57775
setTitle()
}
txtValue.text = ""
tblData.reloadData()
}


}

output:

enter image description here

关于ios - 将文本字段值保存到单元格文本标签(名称)[SWIFT],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44984608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com