【问题标题】:Swift - How to get row data from custom UITableViewCell on UIImageView click from within the cellSwift - 如何从单元格内单击 UIImageView 上的自定义 UITableViewCell 获取行数据
【发布时间】:2015-04-13 19:35:24
【问题描述】:

我正在尝试获取与 UITableViewCell 关联的数据,其中在单元格中单击了 UIImageView。我目前必须捕获点击事件的代码工作正常。但是,一旦我进入被调用的 click 函数,我就无法从单击 UIImageView 的同一 UITableViewCell 中检索相关数据。这是我用来设置点击事件的代码。此代码包含在cellForRowAtIndexPath:

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

这是在单击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)
}

如您所见,我不确定如何引用被点击行的数据。我尝试在UIImageView 上设置标签,但没有成功。我还尝试在UITapGestureRecognizer 上设置标签,但我也无法让它工作。

有谁知道我如何从点击UIImageView 的选定行中引用数据?

【问题讨论】:

    标签: ios uitableview swift uiimageview uitapgesturerecognizer


    【解决方案1】:

    你可能对

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

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

    你还可以使用

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

    如果您只希望图像可点击,则获取被点击的单元格

    此代码还将返回一个 indexPath,您将能够再次使用 indexPath.row 来获取该行。
    如果认为有必要,获取行将使您能够从数组中获取数据

    【讨论】:

    • 感谢您的回复。我会试一试并报告结果。
    • 据我所知,仅当我想捕获仅在行上而不是行内 UIImageView 上的事件单击时,使用 didDeselectRowAtIndexPath 才会有用。我对么?如果没有,我如何使用 didDeselectRowAtIndexPath 捕获对 UIImageView 的点击?
    • 你是对的,希望我对答案的更新对你有帮助
    • @play2win 这真的对你有用吗?这个表达式,tappedView.superview as UITableView 不应该起作用。图像视图的超级视图当然不是表格视图——它甚至不是单元格。它的 superview 应该是单元格的 contentView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多