【问题标题】:button click function in custom cell based on obtained indexpath of current cell根据获得的当前单元格的索引路径,自定义单元格中的按钮单击功能
【发布时间】:2016-05-13 15:07:26
【问题描述】:

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

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

我做过的事情,

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

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

  3. 一键通话功能。

  4. 另一个按钮是在地图套件中获取方向。

  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)

    })

视图控制器导航没有问题。

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

请任何人帮助我。

【问题讨论】:

  • 如果我理解您可以尝试将 GetDirection 中的 indexPath 作为参数传递

标签: iphone swift uitableview custom-cell nsindexpath


【解决方案1】:

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

在您的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)
    })
}

这是你期望的结果?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多