0

I am building a kind of database and for this reason I have created a table view with custom cells so that users can insert the requested data into the appropriate text field. Here it is how it looks like till now.

Database app

Here it is the code that I have written for achieving this.

class customCell1: UITableViewCell {

    @IBOutlet weak var cc1TextField1: UITextField!
    @IBOutlet weak var cc1TextField2: UITextField!
    @IBOutlet weak var cc1TextField3: UITextField!

    @IBOutlet weak var cc1Label1: UILabel!
    @IBOutlet weak var cc1Label2: UILabel!
    @IBOutlet weak var cc1Label3: UILabel!

    @IBOutlet weak var cc1MainLabel: UILabel!

}

In short, in a Swift file I have created a class, which I named customCell1, while in the ViewController, which I called InputDataController, I inserted these lines of code, where inputDataString is the vector of strings concerning the different title that has to be assigned to each label in each row.

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

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

return inputDataString.count

}

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

            let cell1 = tableView.dequeueReusableCell(withIdentifier: "myCell1", for: indexPath) as? customCell1

            cell1?.cc1MainLabel.text = inputDataString[indexPath.row]  

            cell1?.cc1Label1.text = "Fabrics"
            cell1?.cc1Label2.text = "Fittings"
            cell1?.cc1Label3.text = "Furniture"

            cell1?.cc1TextField1.placeholder = "Insert value here"
            cell1?.cc1TextField2.placeholder = "Insert value here"
            cell1?.cc1TextField3.placeholder = "Insert value here"

            return cell1!

        }

Now my question is, once the user added all these values in the text fields and clicked on the save button that I added on the navigation bar, how can I get all the data from the various textfields and assign them to a variable(s) so that I can use those values for some computation. And moreover, where do I add these lines of code so that everything works fine.

picciano
  • 22,341
  • 9
  • 69
  • 82

3 Answers3

0

You can get all data by for in action of button in the navigation bar:

let fabrics = [String]()
let fittings = [String]()
let furnitures = [String]()
for index in 0 ... inputDataString.count-1 {
    fabrics.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField1.text)
    fittings.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField2.text)
    furnitures.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField3.text)
}
Arash Etemad
  • 1,827
  • 1
  • 13
  • 29
  • If a copy what you have written (except the variables, which I inserted in another .swift file) inside the IBAction func saveButton(_ sender: UIBarButtonItem) { ... } I still have errors. Particularly, I get the error "Ambiguous reference to member 'tableView(_numberOfRowsInSection:)'". How can i solve this? What do I have to copy exactly inside the IBAction and what outside? Thanks for your help – Daniele Rocchi Apr 23 '18 at 21:12
  • see this https://stackoverflow.com/a/33724276/7629882, or this https://stackoverflow.com/a/40165696/7629882 – Arash Etemad Apr 24 '18 at 04:31
0

Inside of your IBAction for the Save Button (Assuming that your tableView is in the same class as the IBAction) you can iterate through every visible cell, like what is talked about here and then inside of the iteration code you can insert into whatever persistant storage you would have. for example:

@IBAction func saveButtonTapped(_ sender: Any) {
    let cells: [CustomCell1] = self.tableView.visibleCells as? [CustomCell1] ?? []
    for cell in cells {
        //Do the saving for each cell
    }
}

Keep in mind that tableViews do not keep track of any cells that are not visible because they are recycled. If you need to get more data than what is in the visible cells you will have to somehow save the info of the cell into a temporary array or your tableview Datasource when the cell is no longer visible (a good place may be in the tableView function didEndDisplayingCell) and then you can iterate through the datasource or array when you need to save.

I Hope this helps ^^

Pasosta
  • 825
  • 3
  • 13
  • 22
0

First off, you should connect the button on the navigation bar to code using an @IBAction. See the Apple Tutorial for how to do this:

@IBAction func saveButtonPressed(_ sender: UIButton) {

}

Next, in this function you have just created, you can write the following code to use these values:

//Create three arrays for the three categories of values:
var fabrics: [String] = []
var fittings: [String] = []
var furniture: [String] = []

//Create indexPath for looping through cells in tableview:
var index = IndexPath(row: 0, section: 0)

//Loop through cells, collecting all values from text fields and adding them to the arrays:
for row in 0..<inputDataString.count {
    index = IndexPath(row: row, section: 0)

    fabrics.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField1.text!)
    fittings.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField2.text!)
    furniture.append((tableView.cellForRow(at: index) as! customCell1).cc1TextField3.text!)
}

Now you can use these values however you want.

Xcoder
  • 1,433
  • 3
  • 17
  • 37
  • If a copy what you have written (except the variables, which I inserted in another .swift file) inside the IBAction func saveButton(_ sender: UIBarButtonItem) { ... } I still have errors. Particularly, I get the error "Ambiguous reference to member 'tableView(_numberOfRowsInSection:)'". How can i solve this? What do I have to copy exactly inside the IBAction and what outside? Thanks for your help – Daniele Rocchi Apr 22 '18 at 21:32
  • @DanieleRocchi Are you sure you don't get the error without my code? My code doesn't seem to have anything to do with this method. – Xcoder Apr 22 '18 at 22:00
  • Definitely, without your code everything works fine, even if without the save button, but when I try to copy your code, as well as the ones proposed in the other answers, I get this strange error. Could I sent you the code so that you can tell me if something is wrong? – Daniele Rocchi Apr 23 '18 at 06:33
  • Hmm...can you show me your view controller class definition? – Xcoder Apr 23 '18 at 21:37
  • I think I know your problem. Create an outlet called `tableView` from your table view in storyboard, and connect it to the code(`@IBOutlet`). – Xcoder Apr 23 '18 at 21:39
  • @DanieleRocchi Let me know if you have any further questions :) – Xcoder Nov 26 '18 at 01:10