单击 UITableViewCell 时执行操作

O.O*_*ier 1 uitableview swift swift2 tvos

我正在为 Apple TV 制作 tvOS 应用程序,但我的UITableView. 当我点击一个UITableViewCell什么都没有发生。我正在使用,targetForAction但它似乎不起作用。

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
    self.tableView.delegate = self
}

let allData = ["Los Angeles","New York","San Fransisco"]

@IBOutlet weak var tableView: UITableView!

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return allData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .Default, reuseIdentifier: nil)
    cell.textLabel?.text = "\(allData[indexPath.row])"
    cell.targetForAction("buttonClicked:", withSender: self)
    return cell
}

func buttonClicked(sender:AnyObject) {
    print("button clicked!")
}
Run Code Online (Sandbox Code Playgroud)

O.O*_*ier 8

我找到了解决方案。我不得不添加这个功能:

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