I want to implement a custom UITableViewCell class that is made up of subviews such as, labels, buttons, and images. These cells will display content fetched from the web using an API.
I do not want to implement the UITableView delegate method didSelectRowAtIndexPath
as this will make the entire cell selectable. Only the button in the cell should be able to trigger any action.
The button is connected from the storyboard to the custom UITableViewCell via IBOutlet. The addTarget(_:action:forControlEvents:)
method is called on the UIButton in cellForRowAtIndexPath
of the UITableViewController class.
The roadblock occurs when we want to detect the indexPath
of the selected cell in the Selector function of the button.
This is how the indexPath
for the selected cell can be detected in the Selector function
@IBAction func doSomething(sender: AnyObject) {
var location: CGPoint = sender.convertPoint(CGPointZero, toView: self.tableView)
var indexPath: NSIndexPath = self.tableView.indexPathForRowAtPoint(location)!
println("The indexPath for Selected Cell is - \(indexPath.row)")
}
Although, this successfully gets me around the issue, my question is;
1) Have you found an alternate way of being able to use UIButtons to pass selected cell data in a custom UITableViewCell?
2) What would be the best practice, so far, in Swift to implement a similar scenario?
AnyObject
so callingconvertPoint:toView
without casting it to a UIView would result in a compile error. – Jawwad@IBAction func
mentioned above works without any such errors. Although, making 'sender' of the typeUIView
would be a comparatively cleaner approach. – Lester