【发布时间】: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