我正在使用一个 TableViewController,它有一个包含 2 个静态单元格部分的表格。这嵌入在 View Controller 中。当我点击单元格时,我无法让 didSelectRowAtIndexPath 运行。我已经检查了 this 中的所有常见嫌疑人。问题以及this一。当我尝试在带有动态表的 View Controller 中使用 TableView 时,我能够让它正常工作。使用带有不允许使用 didSelectRowAtIndexPath 的静态单元格的 TableViewController 是否存在问题?
这是我在 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 中是否可行?我找到了一个使用带有 tableviewcontroller 和静态单元格的 Swift 2.2 的工作示例 here .也许在使用 Swift 3 时存在错误?
最佳答案
哇,事实证明 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)!")
}
关于ios - TableViewController 使用静态单元格检测行点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39981106/