简体   繁体   中英

Swift: UITableView not loading data

I have two view controllers FirstViewController and SecondViewController . SecondViewController is a normal view controller with a UITableView on it.

From the FirstViewController I call the second to appear. When I call it I want to pass data via a delegate. The delegate protocol is as follows:

protocol CalculationDelegate{
    func calculations(calculation: [Double])
}

Basically it is passing an array of doubles. When A button is clicked on FirstViewController I use prepareForSegue to get ready for the transition and set up the delegate.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var secondVC = SecondViewController()
    secondVC = segue.destinationViewController as SecondViewController

    // Set up the delegation, so data is passed to SecondViewController
    self.delegate = SecondViewController()
    self.delegate?.calculations(wage.allCalculations)
}

Now in the SecondViewController I want to access the data I have just passed to be loaded on the UITableView . Here is the full class declaration of SecondViewController

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, CalculationDelegate {

    @IBOutlet
    var tableView: UITableView!
    var calculations: [Double] = []

    override func viewDidLoad() {
        super.viewDidLoad()

        self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    // Conform to CalculationsDelegate by implementing this method
    func calculations(calculation: [Double]) {
        self.calculations = calculation

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

        cell.textLabel?.text = NSString(format: "%.2f", self.calculations[indexPath.row])
        return cell
    }

}

As you can see I am trying to assign self.calculations = calculation in the delegates method. Then use that as the data to load the UITableView with, however, the data will not load. I have error checked and the data is being passed from the delegate. Any help would be much appreciated.

SecondViewController() actually contructs a new instance of the view controller. Lets look at your code.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var secondVC = SecondViewController() // Line 1
    secondVC = segue.destinationViewController as SecondViewController()  // Line 2

    // Set up the delegation, so data is passed to SecondViewController()
    self.delegate = SecondViewController()  // Line 3
    self.delegate?.calculations(wage.allCalculations)
}

Take a look at Line1. You are creating a new instance of the the SecondViewController in secondVC which is not required.

Line 2 - This line is required to take instance of the destinationViewController of the segue.

Line 3 = This line you are again creating another instance which is not required. You are setting the calculations array on this instance. The segue works on another instance. which is why you don't get the array inside the second view controller.

This following code should work.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    var secondVC = segue.destinationViewController as SecondViewController()

    secondVC.calculations(wage.allCalculations)
}

I don't see why you need a delegate and a protocol just to set the calculations array. Just defining the calculations function in SecondViewController is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM