【发布时间】:2018-03-20 14:27:06
【问题描述】:
我有一个由UITableView 调用的function,它根据它包含的内容更新钱包值(因此由tableView 调用)。这是WalletViewController 的一部分,用户可以在其中更新他的钱包所包含的内容。
我希望能够从MainViewController 调用此函数,其中还显示了钱包值,因此当用户刷新数据时它是最新的,目前仅发生钱包值更新功能当你加载 WalletViewController 和 reloadData() 时被调用。
如何才能刷新MainViewController 中的值?
数值更新功能的WalletViewController代码:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
cell.delegate = self
cell.amountTextField.delegate = self
updateCellValueLabel(cell, atRow: indexPath.row)
return cell
}
func updateCellValueLabel(_ walletTableViewCell: WalletTableViewCell, atRow row: Int) {
//... calculations for each wallet's object are happening here ...
CoreDataHandler.editObject(editObject: selectedAmount, amount: amount, amountValue: currentAmountValue)
// <--- calculations result for each object is saved to CoreData
updateWalletValue()
updateWalletLabel()
}
func updateWalletValue() {
var items : [CryptosMO] = []
if CoreDataHandler.fetchObject() != nil {
items = CoreDataHandler.fetchObject()!
// <--- Calculations result are fetched from CoreData
}
total = items.reduce(0.0, { $0 + Double($1.amountValue)! } )
// <--- Objects values are added together to get the wallet's grand total
WalletTableViewController.staticTotal = total
}
func updateWalletLabel() {
walletValueLabel.text = formatter.string(from: NSNumber(value: total))
}
MainViewController钱包更新功能至今:
func updateWalletLabel() {
WalletTableViewController.init().updateWalletValue()
walletValue.text = formatter.string(from: NSNumber(value: WalletTableViewController.staticTotal))
}
我想我应该从核心数据中获取钱包对象的数量并从MainViewController 重新计算以获取最新值?或者有没有更简单的解决方案来调用整个tableView 函数?
【问题讨论】:
-
在 MainViewController 中使用 post 通知和 addObserver。
-
请注意
WalletTableViewController()创建了控制器的一个新实例,它不是故事板中的实例。您需要对控制器的实际引用。 -
@MRizwan33 会是什么样子?
-
@PhillipMills 对不起,您是什么意思?我知道这不是你的意思,但我的核心数据功能不在视图控制器中。
-
如果
WalletTableViewController来自MainViewController,请保留前者的引用。
标签: ios swift function uitableview core-data