簡體   English   中英

Swift:UITableView無法加載數據

[英]Swift: UITableView not loading data

我有兩個視圖控制器FirstViewControllerSecondViewController SecondViewController是帶有UITableView的普通視圖控制器。

我從FirstViewController調用第二個出現。 當我調用它時,我想通過委托傳遞數據。 委托協議如下:

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

基本上,它傳遞一個雙精度數組。 當在FirstViewController上單擊A按鈕時,我使用prepareForSegue為過渡做好准備並設置委托。

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)
}

現在,在SecondViewController我想訪問剛傳遞給UITableView 這是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
    }

}

如您所見,我正在嘗試在委托方法中分配self.calculations = calculation 然后將其用作加載UITableView的數據,但是不會加載數據。 我檢查了錯誤,並且正在從委托傳遞數據。 任何幫助將非常感激。

SecondViewController()實際上構造了視圖控制器的新實例。 讓我們看看您的代碼。

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)
}

看一下Line1。 您將在SecondViewController中創建SecondViewController的新實例,這不是必需的。

第2行-需要此行以獲取segue的destinationViewController的實例。

第3行=您將再次在此行中創建另一個不需要的實例。 您正在此實例上設置calculations array 該segue在另一個實例上起作用。 這就是為什么您沒有在第二個視圖控制器中獲取數組的原因。

下面的代碼應該工作。

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

    secondVC.calculations(wage.allCalculations)
}

我不明白為什么只需要一個委托和一個協議來設置計算數組。 僅在SecondViewController中定義calculations功能就足夠了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM