Swift -如何从单元格内的UIImageView上的自定义UITableViewCell获取行数据

时间:2021-10-29 08:23:51

I’m trying to get data associated with the UITableViewCell where a UIImageView is clicked within the cell. The code I currently have to capture the click event is working fine. However, once I get into the click function that is called, I’m unable to retrieve the associated data from the same UITableViewCell where the UIImageView was clicked. Here is the code I'm using to set up the click event. This code is contained in cellForRowAtIndexPath:

我在尝试获取与UITableViewCell相关联的数据在那里UIImageView在单元格中被点击。我目前捕获单击事件的代码运行良好。但是,一旦我进入被调用的click函数,我就无法从UIImageView被单击的同一个UITableViewCell中检索相关数据。这是我用来设置单击事件的代码。此代码包含在cellForRowAtIndexPath中:

    var tap = UITapGestureRecognizer(target: self, action: Selector("staticMap_click:"))
    cell.imgStaticMap.addGestureRecognizer(tap)
    cell.imgStaticMap.userInteractionEnabled = true

Here is the function staticMap_click that gets called when the UIImageView is clicked:

这是在点击UIImageView时调用的函数staticMap_click:

func staticMap_click(sender:UITapGestureRecognizer)
{
    let rowData: NSDictionary = self.arrayPosts[sender.valueForKey("row") as Int] as NSDictionary

    mdblStaticLat = rowData["dblLat"] as String
    mdblStaticLong = rowData["dblLong"] as String
    self.performSegueWithIdentifier("sgShowMapFromStaticClick", sender: self)
}

As you can see, I'm unsure of how to reference the data for the row that was clicked. I attempted setting a tag on the UIImageView, but that didn’t work. I also attempted to set a tag on the UITapGestureRecognizer, but I haven’t been able to get that to work either.

如您所见,我不确定如何引用已单击的行的数据。我尝试在UIImageView上设置一个标签,但是没有成功。我还试图在UITapGestureRecognizer上设置一个标记,但我也没能让它发挥作用。

Does anyone know how I can reference the data from the selected row where the UIImageView is tapped?

有人知道我如何从选中的行引用UIImageView的数据吗?

1 个解决方案

#1


1  

You might be interested in

你可能会感兴趣

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
}

This function will be called whenever a cell is pressed.
You can access the row which is pressed by using indexPath.row

每当按下单元格时,将调用此函数。可以使用indexPath.row访问按下的行

Furthermore you can use

此外您可以使用

func staticMap_click(sender:UITapGestureRecognizer){
    let tappedView = sender.view as? UIImageView
    let indexPath = (tappedView.superview as UITableView).indexPathForCell(self)
}

To get the cell which was tapped if you want only the Image to be tapable

如果你想要图像是可录制的,就可以得到被点击的单元格。

This code will also return an indexPath, you will be able to use indexPath.row again to get the row.
Getting the row will enable you to get data out of an array if deemed necessary

这段代码还将返回一个indexPath,您将能够使用indexPath。再划一次以得到这一行。如果需要,获取行将使您能够从数组中获取数据

#1


1  

You might be interested in

你可能会感兴趣

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
}

This function will be called whenever a cell is pressed.
You can access the row which is pressed by using indexPath.row

每当按下单元格时,将调用此函数。可以使用indexPath.row访问按下的行

Furthermore you can use

此外您可以使用

func staticMap_click(sender:UITapGestureRecognizer){
    let tappedView = sender.view as? UIImageView
    let indexPath = (tappedView.superview as UITableView).indexPathForCell(self)
}

To get the cell which was tapped if you want only the Image to be tapable

如果你想要图像是可录制的,就可以得到被点击的单元格。

This code will also return an indexPath, you will be able to use indexPath.row again to get the row.
Getting the row will enable you to get data out of an array if deemed necessary

这段代码还将返回一个indexPath,您将能够使用indexPath。再划一次以得到这一行。如果需要,获取行将使您能够从数组中获取数据