【问题标题】:TableViewController Detect Row Tap with Static CellsTableViewController 使用静态单元格检测行点击
【发布时间】:2017-02-20 05:20:48
【问题描述】:

我正在使用一个 TableViewController,它有一个包含 2 个静态单元格部分的表格。这是嵌入在视图控制器中的。当我点击单元格时,我无法运行 didSelectRowAtIndexPath。我已经从this 问题和this 一个问题中检查了所有常见的嫌疑人。当我尝试在带有动态表的视图控制器中使用表视图时,我能够让它正常工作。将 TableViewController 与不允许使用 didSelectRowAtIndexPath 的静态单元格一起使用是否存在问题?

这是我在 TableViewController 的自定义类中的内容:

import UIKit

class OptionTableViewController: UITableViewController {


@IBOutlet var optionsTable: UITableView!


let numberOfRows = [7,2]

let cellIdentifier = "OptionCells"

override func viewDidLoad() {
    super.viewDidLoad()

    self.optionsTable.delegate = self
    self.optionsTable.dataSource = self

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func numberOfSections(in tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 2
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    var rows = 0

    if(section < numberOfRows.count){
        rows = numberOfRows[section]
    }

    return rows

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.row)!")

}
}

更新: 我尝试更换 tableviewcontroller 和它嵌入的 viewcontroller,但我仍然无法让 didSelectRowAtIndexPath 运行。

更新 2: 有谁知道这在 Swift 3 中是否可行?我找到了一个使用 Swift 2.2 和 tableviewcontroller 和静态单元格here 的工作示例。也许 Swift 3 有一个错误?

【问题讨论】:

  • 这里有一些有用的东西可以尝试,您可能会觉得有用。 stackoverflow.com/a/20020180/1655378
  • 谢谢,但实际上我也已经尝试过这些建议。
  • 如何在视图控制器中嵌入 TVC?
  • @pbasdf 在视图控制器内使用来自容器视图的嵌入 segue。

标签: ios uitableview static swift3 didselectrowatindexpath


【解决方案1】:

哇,原来didSelectRowAtIndexPath 在Swift 3 中不再正确。正确的用法现在是didSelectRowAt。除了我偶然发现的this 问题外,我没有在任何地方看到此内容。

这个:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("You selected cell #\(indexPath.row)!")
}

不是这个

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.row)!")
}

【讨论】:

    【解决方案2】:

    您可能连接了错误的表格视图。通常,UITableViewController 在视图属性中有它的 tableView,您不需要以编程方式设置数据源和委托。

    【讨论】:

    • 我仔细检查,甚至“重新连接”它。我想我不需要数据源和委托,但是当我无法让它工作时,我还是把它们放进去。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    • 2014-05-16
    相关资源
    最近更新 更多