我想从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)
}
类WalletTableViewCell:UITableViewCell {
func updateCellValue() {
delegate?.updateCellValueLabel(self)
}
@IBAction func amountTextFieldEntered(_ sender: Any) {
delegate?.cellAmountEntered(self)
delegate?.updateCellValueLabel(self)
}
}
每当我想刷新数据时,如何致电updateCellValueLabel(...)
?
答案 0 :(得分: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))
}
之后如果你想多次调用它,你可以将它添加到一个函数中并调用函数