Reputation: 240
I am making an tvOS app for the Apple TV but I have some problems with my UITableView
.
When I click on a UITableViewCell
nothing happens. I am using targetForAction
but it doesn't seem to work.
This is my code:
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!")
}
Upvotes: 2
Views: 10081
Reputation: 21
For me copy and pasting any of these methods did not work because you need to override the method, and if you try to add the override keyword in front of any of the above methods I got the following error:
Declaration 'tableView(_:didSelectRowAtindexPath:)' has different argument labels from any potential overrides
The solution was to use XCode to add this method yourself from Auto Suggest, so start typing
override tableView
and then use the drop down to select the method with the didSelectRowAtindexPath text in it.
Inserting this method this way works for me and maybe, is what you also need to do.
Good luck
Upvotes: 0
Reputation: 188
Swift 5:
func tableView(_ tableView: UITableView, didSelectRowAtindexPath indexPath: IndexPath) {
print(indexPath.row)
}
Upvotes: 3
Reputation: 1199
Please use the UITableView
Delegate method.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
Upvotes: 0
Reputation: 240
I found the solution. I had to add this function:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("selected cell \(indexPath.row)")
}
Upvotes: 9