基于获得的当前单元格的索引路径在自定义单元格中的按钮单击功能

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

我需要知道如何在自定义单元格中编写 segue 代码或控制器代码。

如果我们不能那样做,我如何在该自定义单元格内单击按钮时获取特定自定义单元格的索引路径。

我做过的事,

  1. a ViewController with UITableView 在自定义单元格上填充来自 api 的值。

  2. 自定义单元格包含一些详细信息行和两个按钮。

  3. 一键调用phone函数

  4. 另一个按钮是在 map-kit 中获取方向。

  5. 为api而来,它具有进行phone呼叫和获取方向的值(它包含纬度和经度)。

  6. 来自自定义单元格 class 我正在执行 phone 调用功能(没问题。)。

  7. 我只有获取方向功能有问题。

这里是问题的解释,

代码:

我的自定义单元格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
}
}

在上面的代码中,基本显示功能正在运行。

我已经在 cellForRowAtIndexPath 中编写了 UIButton 单元格点击功能

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

像上面那样。(注意:ContainerViewController 是我的 ViewController,其中包含带有此自定义单元格的 tableView。)

GetDirection() 包含

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)

    })

我对视图控制器导航没有任何问题。

我需要设置 DestinationLocation 的值,所以我需要获取按钮单击单元格的 indexPath。

请任何人帮助我。

有一个简单的方法可以完成这项工作:

在您的 cellForRowAtIndexPath 方法中,您可以使用当前行值为您的按钮设置标签,例如:

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)
}

以及你的功能

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)
    })
}

这是您期望的结果吗?