2015-12-20 54 views
0

我正在爲Apple TV製作tvOS應用程序,但我的UITableView存在一些問題。 當我點擊一個UITableViewCell什麼都沒有發生。我正在使用targetForAction,但它似乎不起作用。單擊UITableViewCell時執行操作

這是我的代碼:

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!") 
} 
+0

你是否有從你的UITableView的'UITableCell':s(或其子類)到下一個視圖的segue?通常,您通過故事板執行此操作,並通過重載表視圖控制器源代碼中的prepareForSegue方法來準備與下一個視圖的可能通信。 < - 既然你自己修好了,那就別管它了! – dfri

回答

3

我找到了解決辦法。 我不得不添加此功能:

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

請使用UITableView委託方法。

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

}