从文本字段添加entires到UITableView自动填充

问题描述:

这是我到目前为止的代码。我可以进入文本框并将其显示在UITableView中,但只有在我重新加载页面并返回之后。我想要输入到文本框中,当我点击“添加”时,它会自动出现在UITableView中。从文本字段添加entires到UITableView自动填充

@IBOutlet var itemTextField: UITextField! 
@IBOutlet var table: UITableView! 

var items: [String] = [] 

@IBAction func add(_ sender: Any) { 
    let itemsObject = UserDefaults.standard.object(forKey: "items") 

    var items:[String] 

    if let tempItems = itemsObject as? [String] { 
     items = tempItems 
     items.append(itemTextField.text!) 

     print(items) 
    } else { 
     items = [itemTextField.text!] 
    } 

    UserDefaults.standard.set(items, forKey: "items") 
    itemTextField.text = "" 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.view.endEditing(true) 
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    textField.resignFirstResponder() 

    return true 
} 

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell") 

    cell.textLabel?.text = items[indexPath.row] 
    cell.textLabel?.font = UIFont(name: "Type02", size: 20) 

    return cell 
} 

override func viewDidAppear(_ animated: Bool) { 
    let itemsOBject = UserDefaults.standard.object(forKey: "items") 

    if let tempItems = itemsOBject as? [String]{ 
     items = tempItems 
    } 

    table.reloadData() 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     items.remove(at: indexPath.row) 

     table.reloadData() 

     UserDefaults.standard.set(items, forKey: "items") 
    } 
} 
+0

刷新表,则刚过你在数组中追加数据,而不是做在viewDidAppear。 –

+0

我试过这个解决方案,但没有运气......我必须回去一个ViewController并回到ViewController与文本字段和TableView,然后才会出现...任何其他想法... – LeiLei1

它适用于我。

添加textfieldtableview的插座。

@IBOutlet weak var nametextField: UITextField! 
@IBOutlet weak var dataTableView: UITableView! 

添加IBAction添加行动button

@IBAction func addNameToTable(_ sender: Any) { 

     // Check for value in nameTextField 
     guard let profilename = nametextField.text else{ 
      return 
     } 
     // Append text field value in data array 
     data.append(profilename) 
     // Reload table to show text field name in table 
     dataTableView.reloadData() 
    } 

创建array持有字符串值 - :

var data = [String]() 

表上添加table方法来填充数据上单击添加按钮。

func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    }// Default is 1 if not implemented 

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

    // dequeue cell 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
     cell.textLabel?.text = data[indexPath.row] 
     return cell 
    } 

    // Row height 
    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 
     return 50 
    } 

完整的代码 - :在附加功能

import UIKit 
import AVKit 
class ViewController: UIViewController { 

    @IBOutlet weak var nametextField: UITextField! 
    @IBOutlet weak var dataTableView: UITableView! 
    var data = [String]() 

    //MArk-: ViewDidLoad 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    @IBAction func addNameToTable(_ sender: Any) { 

     guard let profilename = nametextField.text else{ 
      return 
     } 
     data.append(profilename) 
     dataTableView.reloadData() 
    } 
} 

extension ViewController : UITableViewDelegate,UITableViewDataSource{ 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    }// Default is 1 if not implemented 

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


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{ 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell") 
     cell.textLabel?.text = data[indexPath.row] 
     return cell 
    } 

    public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{ 
     return 50 
    } 
} 
+0

真棒!!!!!十分感谢你的帮助! – LeiLei1

+0

@ LeiLei1如果有帮助,请接受答案。:) –

无论何时更改i​​tems数组,您都需要将表视图告诉reloadData()。因此,请尝试将table.reloadData()添加到add函数的末尾。

+0

我试过这个解决方案,但没有运气...我必须回去一个ViewController并回到与Textfield和TableView的ViewController,然后才会出现...任何其他想法... – LeiLei1