【问题标题】:Perform action when UITableViewCell is clicked单击 UITableViewCell 时执行操作
【发布时间】:2016-03-26 22:12:51
【问题描述】:

我正在为 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!")
}

【问题讨论】:

  • 您是否有从您的UITableViewUITableCell:s(或其子类)到下一个视图的segue?通常,您通过情节提要执行此操作,并通过重载表视图控制器源中的prepareForSegue 方法来准备与下一个视图的可能通信。

标签: swift uitableview swift2 tvos


【解决方案1】:

斯威夫特 5:

func tableView(_ tableView: UITableView, didSelectRowAtindexPath indexPath: IndexPath) {
    print(indexPath.row)
}

【讨论】:

    【解决方案2】:

    请使用UITableView委托方式。

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    
    }
    

    【讨论】:

      【解决方案3】:

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

      func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
          print("selected cell \(indexPath.row)")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多