ios - 使用 swift 在 UITableView 中显示文本字段文本

标签 ios uitableview swift

我对快速编程很陌生。 我只是尝试 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/

相关文章:

ios - 以编程方式将 UIButton 添加到 UITableView 但按钮不显示

ios - 选择行时更改 UIImageView 颜色,如 UILabel 的突出显示属性?

swift - Core Graphics 不绘制某些线条

ios 7 uitableview 单元格长按更改核心数据属性的值

ios - 数组索引超出范围

ios - UIImageView 旋转后高宽变化

iOS 后台蓝牙事件

ios - UITableView - 更改部分标题颜色

ios - 使用 Swift 以编程方式加载图像

ios - 为什么这张图片不适合屏幕尺寸?