TableViewController 使用静态单元格检测行点击

TableViewController Detect Row Tap with Static Cells

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

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

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 设置为 运行.

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

有错误

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

哇,原来 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)!")
}