gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:54 28 4
gpt4 key购买 nike

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

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

我做过的事

  1. 带有 UITableView 的 ViewController,用于在自定义单元格上填充来自 api 的值。

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

  3. 一个按钮是调用电话功能。

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

  5. 为api而来,它有打电话和获取方向的值(它包含纬度和经度)。

  6. 我正在从自定义单元类执行电话调用功能(这没问题。)。

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

这里是问题的解释,

代码:

我的自定义单元格类

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)

})

我对 View Controller 导航没问题。

我需要设置 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)
})
}

这是您期望的结果吗?

关于iphone - 基于获得的当前单元格的索引路径在自定义单元格中的按钮单击功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213504/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com