Jorgen
Jorgen

Reputation: 475

Swift 2 button in UITableViewCell

I'm trying to figure out how to know which cell got it's button tapped.

I have a download button in each cell, and I need to know which cell's button got pressed so I can start a download of the correct item.

I have done it before in OBJ-C using blocks, but just can't get it together in Swift 2.

Anyone out there with a bit of code to share?

Upvotes: 0

Views: 361

Answers (1)

LastMove
LastMove

Reputation: 2482

You can still use your objective-c approach (blocks == swift closures). There is a lot of ways to do what you need. You can detect which row was pressed with this:

    func buttonTapped(sender:UIButton)
{
    let touchPoint = sender.convertPoint(CGPoint.zero, toView:tableView); // get the location of the button inside the tableview 
    let indexPath = tableView.indexPathForRowAtPoint(touchPoint); // ask tableview which row is at this point 
    println(indexPath); // it's works! :)
}

Hope it helps.

Upvotes: 1

Related Questions