我对快速编程很陌生。
我只是尝试 textfield 的一些功能。
只想在 UItableView
中显示文本字段值。
卡在这段代码中
请检查一下......
并给出解决方案
import UIKit
class langViewController: UIViewController {
var txt = ""
let simpleTableIdentifier = "TableViewCell";
@IBOutlet weak var txtlang: UITextField!
@IBOutlet weak var langTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func addButtonTapped(sender: AnyObject)
{
txt = txtlang.text
self.langTableView.reloadData()
langData.append(txt)
print(langData)
txtlang.resignFirstResponder()
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as UITableViewCell
cell.textLabel?.text = txt
return cell;
}
最佳答案
要更新您的表格 View 单元格,您必须调用 reloadData()
。所以你必须做这样的事情:
1. 在类定义开始的地方声明 txt
变量:
var txt = ""
2. 在您的按钮处理程序中调用 reloadData()
来更新您的单元格。
@IBAction func addButtonTapped(sender: AnyObject) {
txt = txtlang.text
self.tableView.reloadData()
}
3. 将文本设置为您的单元格标签:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) as! UITableViewCell
cell.textLabel?.text = txt
return cell;
}
关于ios - 使用 swift 在 UITableView 中显示文本字段文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30123940/