在另一个视图中使用需要 UITableView 的函数?



在我的WalletTableViewController中,我有两个函数,用于计算Wallet Value

A.updateCellValue()reloadData()tableView调用,并使用indexPath.row获取与单元格对应的value(价格)和amount(硬币数量),并进行计算以获得该硬币的总价值(金额价值 = 价值 * 金额)。然后将其与核心数据一起保存。

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
updateCellValue(cell, atRow: indexPath.row)
return cell
}
func updateCellValue(_ walletTableViewCell: WalletTableViewCell, atRow row: Int) {
var newCryptos : [CryptosMO] = []
var doubleAmount = 0.0
if CoreDataHandler.fetchObject() != nil {
newCryptos = CoreDataHandler.fetchObject()!
}
cryptoPrice = cryptos[row].code!
guard let cryptoDoublePrice = CryptoInfo.cryptoPriceDic[cryptoPrice] else { return }
let selectedAmount = newCryptos[row]
guard let amount = selectedAmount.amount else { return }
var currentAmountValue = selectedAmount.amountValue
doubleAmount = Double(amount)!
let calculation = cryptoDoublePrice * doubleAmount
currentAmountValue = String(calculation)
CoreDataHandler.editObject(editObject: selectedAmount, amount: amount, amountValue: currentAmountValue)
updateWalletValue()
}

B.updateWalletValue()是一个函数,它获取核心数据中的所有amountValue对象并将它们相加以计算Wallet Value

func updateWalletValue() {
var items : [CryptosMO] = []
if CoreDataHandler.fetchObject() != nil {
items = CoreDataHandler.fetchObject()!
}
total = items.reduce(0.0, { $0 + Double($1.amountValue)! } )
WalletTableViewController.staticTotal = total
}

在我的MainViewController中,Wallet Value也会显示,但是我该如何刷新它的值?

func updateMainVCWalletLabel() {
//... what can I do here??
}

这当然适用于TableViewindexPathWalletViewController,但是如何从MainViewController调用updateCellValue以保持值更新?

WalletViewController被实例化并从MainViewController推送:

@IBAction func walletButtonTapped(_ sender: Any) {
let walletViewController = self.storyboard?.instantiateViewController(withIdentifier: "walletTableViewController")
self.present(walletViewController!, animated: true)
}

如果要在多个视图控制器中使用单个方法,则应实现该方法,以便从任何地方调用该方法。例如,您可以在此处使用单例类。

  1. 创建一个快速文件并按照您的意愿命名(如钱包助手或钱包管理器)
  2. 然后你会得到一个具有以下格式的文件
class WalletHelper: NSObject
{
}
  1. 为该类创建共享实例
static let shared = WalletHelper()
  1. 实现你想要的方法
func getWalletValue() -> Float {
// write your code to get wallet value`
// and return the calculated value
}
  1. 最后调用该方法,如下所示
let walletValue = WalletHelper.shared. getWalletValue()

钱包助手.swift看起来像

import UIKit
class WalletHelper: NSObject
{
static let shared = WalletHelper()
func getWalletValue() -> Float {
// write your code to get wallet value
// and return the calculated value
}
}

更新(下面的旧答案)

对我来说,绝对不清楚你想要实现什么:你想更新哪个值?staticTotal? 对我来说,这似乎是一个像XYProblem一样的点亮。正如@vadian昨天评论的那样,请清楚地描述数据的存储位置,控制器的连接方式,要更新什么以实现什么。您还可以提供一个MCVE,明确您的要求,而不是添加越来越多的代码片段。

而且,更有趣的是:当您在tableView(_: cellForRowAt:)的调用堆栈中时,为什么要修改CoreData 条目 (CoreDataHandler.editObject)?永远不要这样做!您处于读取案例中 -reloadData旨在更新表视图以反映数据更改的数据更改。它打算更新数据本身。tableView(_: cellForRowAt:)被多次调用,尤其是当用户上下滚动时,因此当您写入数据库时,会造成较大的写入影响(因此:性能损失)。


旧帖子

您只需在表视图上调用reloadData,然后它将更新其单元格。

您的代码也存在一些问题:

  • 你为什么这么频繁地打电话给updateWalletValue()?每次显示一个单元格时,它都会被调用,运行整个数据库并做一些约简工作。您应该缓存该值,并且仅在数据本身被修改时才更新它
  • 你为什么给fetchObject()打电话两次(updateWalletValue())?

您应该这样做:

func updateWalletValue() {
guard let items:[CryptosMO] = CoreDataHandler.fetchObject() else {
WalletTableViewController.staticTotal = 0.0
return
}
total = items.reduce(0.0, { $0 + Double($1.amountValue)! } )
WalletTableViewController.staticTotal = total
}

最新更新