【问题标题】:Tableviewcell calling uiview controller function not working swift iosUitableviewcell调用uiviewcontroller函数不起作用swift ios
【发布时间】:2017-04-20 11:41:08
【问题描述】:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   cell.viewcontroller = self
}

func reloadData(){
    // reload function here, so when called it will reload the tableView
    self.notificationTable.reloadData()
}

Tableview 单元格按钮代码

@IBAction func deleteRecord(_ sender: Any) {
    self.viewController.reloadData() //<-- 
}

遇到错误

@

NotificationTableViewCell.swift:64:18: 类型值 'UIViewController' 没有成员 'reloadData'

【问题讨论】:

  • 在这里self.viewController.reloadData()使用你的tableview名称self. notificationTable.reloadData()
  • 为什么你在 tableViewCell 中声明了 deleteRecord() ?
  • 你查过max的答案了吗?

标签: ios swift uitableview uiview


【解决方案1】:

试试这个(self.viewController as? YourViewControllerClass).reloadData()UIViewController 没有 reloadData() 方法 - 只有你的子类有。

【讨论】:

    【解决方案2】:

    你需要使用 NSNotificationCenter 调用 reload 方法

    // 在你的 ViewController 的 ViewDidLoad 中声明

    // Register to receive notification
        NotificationCenter.default.addObserver(self, selector: #selector(YourClassName. reloadData), name: Notification.Name("NotificationIdentifier"), object: nil)
    

    在 TableViewCell 中

    @IBAction func deleteRecord(_ sender: Any) {
            // Post notification
        NotificationCenter.default.post(name: Notification.Name("NotificationIdentifier"), object: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-13
      • 1970-01-01
      • 1970-01-01
      • 2017-06-07
      • 2019-05-30
      • 1970-01-01
      • 2014-02-19
      相关资源
      最近更新 更多