我有一个由function
调用的UITableView
,它根据它包含的内容更新钱包值(因此通过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
函数?
答案 0 :(得分:2)
有几种方法可以做到这一点。如果您想要“发布通知”路线,@ MRizwan33的答案将会有效。
另一个选项,就是我通常做的,是从MainViewController
获取对WalletViewControllerInstance
的引用,然后直接在class MainViewController: UIViewController {
// Local variable to cache instance of WalletViewController
var walletVC: WalletViewController?
func functionWhereWalletViewControllerIsInstantiated() {
// Save the reference to the WalletViewController instance
walletVC = self.storyboard?.instantiateViewController(
withIdentifier: "WalletViewControllerIdentifier") as!
WalletViewController
}
func someFuncWhereYouWantToUpdateWalletViewController() {
// Call the function on your cached WalletViewController
// instance that performs the refresh
walletVC.doTheUpdate()
}
}
上拨打电话:
walletVC
如果您实例化推送它的func functionWhereWalletVcIsPushed() {
if (walletVC == null) {
walletVC = self.storyboard?.instantiateViewController(
withIdentifier: "WalletViewControllerIdentifier") as!
WalletViewController
}
// Push the view controller
}
,则可以执行以下操作:
lazy var walletVC: WalletViewController = {
return self.storyboard?.instantiateViewController(
withIdentifier: "WalletViewControllerIdentifier") as!
WalletViewController
}
您还可以使该属性变得懒惰:
target
(我不是Swift专家,所以你要仔细检查语法)
答案 1 :(得分:1)
使用您想要呼叫的这一行来更新另一个视图。
let a = Array([[1,2,4],[4,3,1]])
func findMax(on axis: Int, in array: [[Int]]) -> [Int] {
let result: [Int]
if axis == 0 {
result = array.map() {
$0.index(of: $0.max()!)! // Make sure the array is not empty
}
} else {
result = findMax(on: 0, in: transpose(array: array))
}
return result
}
func transpose(array: [[Int]]) -> [[Int]] {
let d1 = array.count
let d2 = array[0].count // Take the length of the first sub array(first row).
var result: [[Int]] = Array(repeating: Array.init(repeating: 0, count: d1), count: d2)
for (k, a) in array.enumerated() {
for (i, v) in a.enumerated() {
result[i][k] = v
}
}
return result
}
findMax(on: 0, in: a) // [2, 0]
findMax(on: 1, in: a) // [1, 1, 0]
添加将在其中进行更新的这些行:
NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)