【发布时间】:2018-03-06 12:52:01
【问题描述】:
我想从ViewWillAppear 触发func updateCellValueLabel(...),以便在我打开视图时我的数据是最新的。
我可能想多了,因为TableViewCell: 参数让我可以访问indexPath,但我不知道如何调用它。
视图控制器:
class WalletTableViewController: UIViewController, UITextFieldDelegate {
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifications()
self.hideKeyboard()
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
tableView.delegate = self
tableView.dataSource = self
if CDHandler.fetchObject() != nil {
cryptos = CDHandler.fetchObject()!
tableView.reloadData()
}
erc20AddressTextField.delegate = self
WalletTableViewCell.init().updateCellValue()
}
override func viewWillAppear(_ animated: Bool) {
tableView.delegate = self
tableView.dataSource = self
if CDHandler.fetchObject() != nil {
cryptos = CDHandler.fetchObject()!
tableView.reloadData()
}
WalletTableViewCell.init().updateCellValue()
}
}
我的表格视图扩展:
extension WalletTableViewController: UITableViewDelegate, UITableViewDataSource, CryptoCellDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cryptos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! WalletTableViewCell
cell.cryptoNameLabel.text = cryptos[indexPath.row].name
cell.cryptoCodeLabel.text = cryptos[indexPath.row].symbol
cell.amountLabel.text = cryptos[indexPath.row].amount
cell.amountTextField.placeholder = cryptos[indexPath.row].placeholder
cell.delegate = self
cell.amountTextField.delegate = self
return cell
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
/*if editingStyle == .delete {
cryptos.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
CDHandler.deleteObject(crypto: cryptos[indexPath.row])
}*/
if editingStyle == .delete {
let selectedManagedObject = cryptos[indexPath.row]
CDHandler.deleteObject(entity:"CryptosMO", deleteObject: selectedManagedObject)
cryptos.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// Value calculation & label update
//----------------------------------
func updateCellValueLabel(_ walletTableViewCell: WalletTableViewCell) {
print("CALLED") // <--- This is never printed
if walletTableViewCell.amountLabel.text == "" {
walletTableViewCell.amountLabel.text = "0.00000000"
}
var newCryptos : [CryptosMO] = []
var doubleAmount = 0.0
var cryptoPrice = ""
let indexPath = tableView.indexPath(for: walletTableViewCell)
if CDHandler.fetchObject() != nil {
newCryptos = CDHandler.fetchObject()!
tableView.reloadData()
}
cryptoPrice = cryptos[(indexPath?.row)!].code!
guard let cryptoDoublePrice = CryptoInfo.cryptoPriceDic[cryptoPrice] else { return }
let selectedAmount = newCryptos[(indexPath?.row)!]
if let amount = selectedAmount.amount {
doubleAmount = Double(amount)!
}
amountValue = cryptoDoublePrice * doubleAmount
if WalletViewController.currencyCode != "" {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = Locale(identifier: "\(WalletViewController.currencyCode)")
walletTableViewCell.cryptoValueLabel.text = formatter.string(from: NSNumber(value: amountValue))
}
}
}
表格视图单元格:
protocol CryptoCellDelegate {
func cellAmountEntered(_ walletTableViewCell: WalletTableViewCell)
func updateCellValueLabel(_ walletTableViewCell: WalletTableViewCell)
}
类 WalletTableViewCell: UITableViewCell {
func updateCellValue() {
delegate?.updateCellValueLabel(self)
}
@IBAction func amountTextFieldEntered(_ sender: Any) {
delegate?.cellAmountEntered(self)
delegate?.updateCellValueLabel(self)
}
}
我想刷新数据时如何调用updateCellValueLabel(...)?
【问题讨论】:
-
你能详细解释一下你现在的场景吗?您要存档什么或您想要的输出是什么。?
-
viewDidLoad 在 viewWillAppear 之前触发。所以最好在 viewDidLoad() 中更新数据
-
@ravi.p 这是一个钱包。你能在代码中找到
updateCellValueLabel吗?该函数获取用户存储在核心数据中的金额,并使用amount * coin value更新钱包值。我希望钱包值保持更新为最新值(在别处获取)。 -
@JD。不幸的是,它也不是从
viewDidLoad()调用的:/ -
当我看到这个
WalletTableViewCell().updateCellValue()时,我记得我说过这个(stackoverflow.com/questions/49047174/…)
标签: ios swift function uitableview core-data