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

标签 ios uitableview static swift3 didselectrowatindexpath

我正在使用一个 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/

相关文章:

iphone - UIView 绘制时的背景色

iphone - 如何在 UITableViewCell 中的类别旁边显示计数?

ios - 如何在 swift 3 中从一个 View Controller 传递到前一个 View Controller 时设置委托(delegate)?

c# - java中public final static的c#等价物是什么

java - Java中静态字段有什么用?

ios - Nil __block 变量设置 NSString

ios - 如何在 Swift for Realm 模型中设置主键

ios - 在 UItableviewCell 上设置的图像

java - 从 Java 中的静态方法获取类名

ios - 前导空格适合 iOS7 和 iOS8