gpt4 book ai didi

ios - Swift:UITableView 未加载数据

转载 作者:行者123 更新时间:2023-11-28 11:21:15 25 4
gpt4 key购买 nike

我有两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com