从`ViewWillAppear`触发`TableViewCell`函数

问题描述 投票:0回答:1

我想从func updateCellValueLabel(...)触发ViewWillAppear,所以当我打开视图时,我的数据是最新的。

我可能正在过度思考这种情况,因为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)

}

class WalletTableViewCell:UITableViewCell {

func updateCellValue() {
    delegate?.updateCellValueLabel(self)
}

@IBAction func amountTextFieldEntered(_ sender: Any) {
    delegate?.cellAmountEntered(self)
    delegate?.updateCellValueLabel(self)
}  

}

每当我想刷新数据时,如何调用updateCellValueLabel(...)

ios swift function uitableview core-data
1个回答
1
投票

func updateCellValueLabel(_ walletTableViewCell:WalletTableViewCell)将WalletTableViewCell作为参数,因此如果不将参数传递给单元格,则无法调用它。因此,如果要在viewDidAppear上调用它,则必须循环tableview并调用它对于每个单元格,所以在视图viewDidAppear中你应该写类似的东西..

for i in 0...self.tableview.numberOfSections - 1{
   self.updateCellValueLabel(_ walletTableViewCell: self.tableview.cellForRow(at: i))
}

之后如果你想多次调用它,你可以将它添加到一个函数中并调用该函数

© www.soinside.com 2019 - 2024. All rights reserved.