0

So, I was trying to make Birthday App which notes name and birthday of the written textField values. When i press button, those values are saved in two separated Arrays. Those names will be seen on the tableView. I can't see nameArray values on cells but the program builds successfully.

My question is how can update tableView cell names with the values of my nameArray?

Thanks in advance

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    
    @IBOutlet weak var nameTF: UITextField!
    @IBOutlet weak var birthdayTF: UITextField!
    @IBOutlet weak var firstTV: UITableView!
    
    var nameArray = [String]()
    var birthdayArray = [String]()
    
    let rowName = UITableViewCell()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        firstTV.delegate = self
        firstTV.dataSource = self
    }

    @IBAction func saveButton(_ sender: Any) {
       
        nameArray.append(nameTF.text!)
        birthdayArray.append(birthdayTF.text!)
        
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       
        rowName.textLabel?.text = nameArray[indexPath.row]
        return rowName
    }
}
gcharita
  • 6,251
  • 3
  • 16
  • 29
cemogolog
  • 3
  • 1
  • `firstTV.reloadData()` after you append the values to the arrays in `saveButton()` func. – DonMag Sep 21 '20 at 20:02
  • Thanks, it did work. When i press the button it does save the value but on the second press it deletes previous one. How can i remain more then one data at the same time? – cemogolog Sep 21 '20 at 20:11
  • Hello, @cemogolog. Welcome to stackoverflow.com, whenever you have a problem, you should first search for it to get your answer before submitting a question. Here are. several answers explain how to use table view in detail. https://stackoverflow.com/questions/33234180/uitableview-example-for-swift – Essam Mohamed Fahmi Sep 21 '20 at 20:52

1 Answers1

1

You need to go through a few tutorials on using table views and reusable table view cells.

I'm going to assume that, in Storyboard, you added a cell to the table view and set its Style to Basic. You need to give that prototype cell an Identifier -- such as "cell":

enter image description here

Then, in your cellForRowAt function, dequeue a reusable instance of that cell.

Here's your modified code:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
    
    @IBOutlet weak var nameTF: UITextField!
    @IBOutlet weak var birthdayTF: UITextField!
    @IBOutlet weak var firstTV: UITableView!
    
    var nameArray = [String]()
    var birthdayArray = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        firstTV.delegate = self
        firstTV.dataSource = self
    }
    
    @IBAction func saveButton(_ sender: Any) {
        
        nameArray.append(nameTF.text!)
        birthdayArray.append(birthdayTF.text!)
        
        firstTV.reloadData()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = nameArray[indexPath.row]
        return cell
    }
}
DonMag
  • 44,662
  • 5
  • 32
  • 56