ios - Swift:UITableView 未加载数据

标签 ios uitableview swift delegates

我有两个 View Controller FirstViewControllerSecondViewControllerSecondViewController 是一个带有 UITableView 的普通 View Controller 。

FirstViewController 我调用第二个出现。当我调用它时,我想通过委托(delegate)传递数据。 委托(delegate)协议(protocol)如下:

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

基本上它传递的是 double 组。当单击 FirstViewController 上的按钮时,我使用 prepareForSegue 为转换做好准备并设置委托(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)
}

现在在 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
    }

}

如您所见,我正在尝试在委托(delegate)方法中分配 self.calculations = calculation。然后将其用作加载 UITableView 的数据,但是数据不会加载。我检查了错误并且数据正在从委托(delegate)中传递。任何帮助将不胜感激。

最佳答案

SecondViewController() 实际上构造了一个新的 View Controller 实例。让我们看看您的代码。

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

看看第 1 行。您正在 secondVC 中创建 SecondViewController 的新实例,这不是必需的。

第 2 行 - 此行需要获取 segue 的 destinationViewController 实例。

第 3 行 = 这行您将再次创建另一个不需要的实例。您正在为此实例设置计算数组。 segue 在另一个实例上工作。这就是为什么您没有在第二个 View Controller 中获取数组的原因。

下面的代码应该可以工作。

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

    secondVC.calculations(wage.allCalculations)
}

我不明白为什么你需要一个委托(delegate)和一个协议(protocol)来设置计算数组。只需在 SecondViewController 中定义 calculations 函数就足够了。

关于ios - Swift:UITableView 未加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27905225/

相关文章:

ios - 使用 AVCaptureSession 录制视频,添加 CIFilter 并保存到相册

swift - 不使用 Alamofire 应用 HTTP POST header 数据

ios - 如何使用最后一个 CoreData 条目验证今天的日期

ios - Swift:在 MapView 上移动注释

ios - 切换 map : error: cannot invoke 'map' with an argument list of type '((_) -> _)'

iphone - 更新放置在 UITableViews 节标题中的 UILabel 的值

ios - 如何取消通知中心的通知(推送通知)

ios - UITableViewCell(可重用单元格)未识别正确的约束

ios - 使用 Firebase 和 Swift 保存多个表部分的数据

swift - 返回自定义 GeneratorType