spikesa spikesa - 6 months ago 41
Swift Question

button click function in custom cell based on obtained indexpath of current cell

I need to know how to write segue code or controller code inside a custom cell.

if we can't do that, how can i get the indexpath of particular custom cell on button click inside that custom cell.

Things i did,


  1. a ViewController with UITableView that populates the value from api, on the custom cell.

  2. custom cell consists of some lines of details and two buttons.

  3. one button is to make phone call function.

  4. another button is to get direction in map-kit.

  5. coming for the api, it has values for making phone call and to get direction(it contains latitude and longitude).

  6. from the custom cell class i'm perform the phone call function(No Problem with this.).

  7. i'm having problem with get direction function only.



here goes the problem explanation,

codes:

my custom cell class

class Usercell: UITableViewCell {

var phoneNumber = "" // get the phone number

var latlng:NSArray = []

@IBOutlet weak var vendorName3: UILabel!

@IBOutlet weak var vendorAdddress3: UILabel!

@IBOutlet var FeaturedDisplayText: UILabel!

@IBOutlet weak var getDirectionButton: UIButton!

override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}


// action method for call button tap
@IBAction func CallbuttonTap(sender: AnyObject) {

let phoneString = "tel://\(phoneNumber)"
let openURL = NSURL(string:phoneString)
if openURL == nil {
return
}
let application:UIApplication = UIApplication.sharedApplication()
if (application.canOpenURL(openURL!)) {
application.openURL(openURL!)
}

}

@IBAction func GetDirectionButtonTapped(sender: AnyObject) {

print("Get direction button tapped.")

print("LatLng for this cell is:",latlng)
}

override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)

// Configure the view for the selected state
}
}


in the above code basics display functions are working.

I have written that UIButton cell click function in cellForRowAtIndexPath

cell1.getDirectionButton.addTarget(self, action: #selector(ContainerViewController.GetDirection(_:)), forControlEvents: UIControlEvents.TouchUpInside)


like that above.(Note: ContainerViewController is my ViewController which contains my tableView with this custom cell.)

GetDirection() contains

let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

let vc: mapvc2 = storyboard.instantiateViewControllerWithIdentifier("mapvc2") as! mapvc2
let indexPath = ???

let DestinationLocation = CLLocationCoordinate2D(latitude: val[indexPath.row].latlng[0] as! Double, longitude: val[indexPath.row].latlng[1] as! Double)

vc.PlotRoute(DestinationLocation)

vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< back", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ContainerViewController.goBack))
let navController = UINavigationController(rootViewController: vc)

dispatch_async(dispatch_get_main_queue(),{

self.presentViewController(navController, animated: true, completion: nil)

})


i'm having no problem with the view controller navigation.

i need to set the value for DestinationLocation, so i need to get the indexPath for button clicked cell.

Please anybody help me out.

Answer

There is a simple way to make this work:

In your cellForRowAtIndexPath method, you can set a tag to your button with the current row value like:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = //cell
    cell.myButton.tag = indexPath.row
    cell.myButton.addTarget(self, action: #selector(ViewController.getDirection(_:)), forControlEvents: UIControlEvents.TouchUpInside)
}

and on your function

func getDirection(sender: UIButton) {
    let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

    let vc: mapvc2 = storyboard.instantiateViewControllerWithIdentifier("mapvc2") as! mapvc2
    let indexPathRow = sender.tag

    let DestinationLocation = CLLocationCoordinate2D(latitude: val[indexPathRow].latlng[0] as! Double, longitude: val[indexPathRow].latlng[1] as! Double)

    vc.PlotRoute(DestinationLocation)

    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "< back", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ContainerViewController.goBack))
    let navController = UINavigationController(rootViewController: vc)

    dispatch_async(dispatch_get_main_queue(),{
        self.presentViewController(navController, animated: true, completion: nil)
    })
}

It is result you expect ?