【问题标题】:Cannot call value of non-function type: UITableView无法调用非函数类型的值:UITableView
【发布时间】:2017-06-27 04:26:38
【问题描述】:

我有一个从 UITableViewController 扩展的 Swift 类。有这个功能

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {...}

有需要,我需要在一个地方以编程方式调用这个函数,当我写的时候

self.tableView(tableView, cellForRowAt: 1)
  • 我明白了,无法调用非函数类型“UITableView!”的值

如何以编程方式调用此函数?

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您不需要(您不应该)直接调用该委托/数据源方法。 UITableView 有一个功能:

    tableView.cellForRow(at: indexPath)
    

    只需根据您的行创建索引路径,例如:

    let indexPath = IndexPath(row: 1, section: 0)
    

    【讨论】:

      【解决方案2】:

      cellForRowAt 采用 IndexPath,而不是 Int

      func someFunc(indexPath: IndexPath) {
          let cell = tableView(tableView, cellForRowAt: indexPath)
          print(cell)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-08-16
        • 2020-02-01
        • 2021-01-15
        • 2021-03-30
        • 2016-03-10
        • 2016-09-26
        • 2019-05-05
        相关资源
        最近更新 更多