Avi
Avi

Reputation: 17

Container View that contains a TableView change number of rows depending number of inputs Swift

I am new to swift as well as creating iOS apps and I thought I would make a simple app that calculates the averages of the numbers inputted into the TextField. The averageViewController also has a container view as well that contains TableView. Once the person has hit the "Next" button I would like the TableView to display the numbers that have been inputted. (each cell label has a single number).

This is my segue method in my averageViewController as well as the function I am using when the user presses the button:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "sendResult" {
    let inputArray = segue.destinationViewController as! averageTableViewController

    inputArray.arrayFromSegue = average.getArray()
    }

}



@IBAction func nextButton(sender: UIButton) {
    average.arrayInput(((inputTextField.text!) as NSString).doubleValue)
    calcAverage()

    inputTextField.text=nil
}

This is the code I made for my averageTableViewController:

class averageTableViewController: UITableViewController {

    var arrayFromSegue = NSMutableArray()
    var arrayUsed = NSMutableArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        arrayUsed = arrayFromSegue

    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var Cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell

        Cell.textLabel?.text = String(arrayUsed[indexPath.row])
        return Cell

    }

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

I have done quite a bit of research and I believe one of my mistakes is that the action segue that I am doing (show) does not produce the correct results.

Upvotes: 0

Views: 1001

Answers (1)

vien vu
vien vu

Reputation: 4337

Problem isn't in segue you do. If you want change number of rows depend on number of input you should update your array data and reload your table. In your case you can change like this:

  1. Create variable hold your tableViewController, in your case can put name is: inputArray

    inputArray = segue.destinationViewController as! averageTableViewController
    
  2. When you tap nextbutton you update array average and assign it to tableViewConroller `inputArray and reload it:

    inputArray.arrayUsed = average.getArray()
    inputArray.tableView.reloadData()
    

If you have any problem don't hesitate ask me. I will help you.

You can check my demo: Demo

Your project leak segue to tableviewcontroller: Please fix project like step below:

  • drage segue from average to averagetable

enter image description here

  • make it is embed:

enter image description here

  • Select it and name it sendResult

enter image description here

Upvotes: 1

Related Questions