add entires from textfield to UITableView to automatically populate

前端 未结 2 488
小鲜肉
小鲜肉 2020-12-21 20:04

This is the code I have so far. I am able to enter into the textfield and have it appear in the UITableView, but only after I reload the page and come back to it. I want for

相关标签:
2条回答
  • 2020-12-21 20:28

    It works for me .

    Add outlet for textfield and tableview.

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

    Add IBAction for Add action 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()
        }
    

    Create array to hold string values-:

    var data = [String]()
    

    Add table methods to populate data on table on clicking Add button.

    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
        }
    

    COMPLETE CODE -:

    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 讨论(0)
  • 2020-12-21 20:34

    You need to tell the table view to reloadData() whenever the items array changes. So try adding table.reloadData() to the end of your add function.

    0 讨论(0)
提交回复
热议问题