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

use*_*916 7 static uitableview didselectrowatindexpath ios swift3

我正在使用一个TableViewController,它有一个包含2个静态单元格的表.它嵌入在视图控制器中.我点击单元格时无法运行didSelectRowAtIndexPath.我已经检查所有常见的犯罪嫌疑人,从这个问题以及一个.当我在带有动态表的viewcontroller中尝试使用表视图时,我能够让它工作得很好.将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)!")

}
}
Run Code Online (Sandbox Code Playgroud)

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

更新2: 有没有人知道这是否可以在Swift 3中使用?我发现使用雨燕2.2与tableviewcontroller和静态细胞工作的例子在这里.也许有一个错误与Swift 3这样做?

use*_*916 6

哇,所以事实证明在Swift 3中didSelectRowAtIndexPath不再正确.正确的用法是nowSelectRowAt.除了我偶然发现的这个问题之外,我没有看到这提到的任何地方.

这个:

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

不是这个:

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


归档时间:

查看次数:

2906 次

最近记录:

9 年 前