How to add additional textfields by clicking button in table view

I am trying to add an option to add additional student fields inside the table so that the user can add more than one student name.

But I am confused about how to do it using the table view.

I am not interested in hiding view with a specific number of fields.

Please don’t downvote if you don’t know the solution.I have searched all over and didn’t get any complete solution.

    class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

struct listItems{

var title : String
var isExpandable:Bool
var maxFields :Int

init(title:String,isExp:Bool,mxF:Int) {

    self.title = title
    self.isExpandable = isExp
    self.maxFields = mxF
}

}

@IBOutlet weak var tblListTable: UITableView!

let data : [listItems] = [listItems(title: “Name”, isExp: false, mxF: 1), listItems(title: “Student Name”, isExp: true, mxF: 20), listItems(title: “Email”, isExp: false, mxF: 1)]

override func viewDidLoad() {
super.viewDidLoad()

tblListTable.delegate = self
tblListTable.dataSource = self

self.tblListTable.reloadData()
print("isLoaded")

}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return data.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

print("cellForRow")

let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! ListCell


cell.lblName.text = data[indexPath.row].title

if data[indexPath.row].isExpandable == true {
    cell.btnAddField.isHidden = false
    print("ishidden")
}
else {
    cell.btnAddField.isHidden = true
}


return cell

}
}

List Cell Class

import UIKit
protocol AddFieldDelegate : class { func addField( _ tag : Int) }

class ListCell: UITableViewCell {

@IBOutlet weak var btnAddField: UIButton!
@IBOutlet weak var lblName: UILabel!
@IBOutlet weak var txtField: UITextField!

override func awakeFromNib() {

super.awakeFromNib()
// Initialization code

}

override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

}

func addField( _ tag : Int){

}

Hi @ArjunMeridian

Hmmm, this is an interesting one. Let me see if I have this right.

You want to add fields to a UI after the app is published by user interaction with a button.

I suppose if you have only one row in your tableView which is populated with the correct fields, you can have the button add a += 1 to the count for rows in the tableView and the reload. You would also need to have the database check for nil when the tableView tries to reload the new row. And then save back to the database after the new row is filled in.

This one is head scratcher for sure. Let me know if this is close to what you are looking to do.

Blessings,
—Mark

PS no up or down votes here, thank goodness. We just try and help each other!!

1 Like

Thank you for your valuable reply. I should definitely try out your solution to my problem. Keep your helping mentality.