joda joda -4 years ago 152
Swift Question

button inside cellView I cann't know which cell user pressed

I have custem cell in tableView
inside it I have a button , I want when user click on the button push viewController, how I can do that and how I can know which cell user use its button, because here not


Answer Source

Add Tag of button in Cell and add Target for your transition Function

func    tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell =     tableView.dequeueReusableCellWithIdentifier(reuseIdentifier) as!   CustomCell
cell.button.tag = indexPath.row
cell.button.addTarget(self, action: #selector(    self.transitonMethod    ), forControlEvents: UIControlEvents.TouchUpInside)
return cell


Fetch indexPath from tag of sender Button and Fetch Cell for this indexPath.Push your controller on your navigation controller

func transitonMethod(sender: AnyObject){
 let indexPath = NSIndexPath.init(index: sender.tag)
 let cell = tableView.cellForRowAtIndexPath(indexPath)

self.navigationController?.pushViewController(yourController, animated: true)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download