【问题标题】:How to know which IndexPath.row is clicked on IBAction, and how to exclude a specific button from it being triggered如何知道在 IBAction 上单击了哪个 IndexPath.row,以及如何从触发中排除特定按钮
【发布时间】:2016-03-31 05:12:11
【问题描述】:

我有一个 UITableViewController。它有一个原型单元,并根据用户查询生成 0 到 100 个单元。加载后,如果用户点击单元格内的任何位置,除了特定按钮之外,我希望触发 IBAction。我有多个标签,如果用户点击它们,我仍然希望触发 IBAction。我该如何做到这一点?

这是我加载表格的代码:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"
        let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
        cell.activityLabel?.text = activityNames[indexPath.row]
        cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
        cell.sloganLabel?.text = slogan[indexPath.row]
        return cell
}

我不想在 tableView 中为 MVC 原则编写所有这些代码。

【问题讨论】:

  • 您可以为此目的使用标签值
  • "EXCEPT for a specific button" - 所以你在单元格中有一个UIButton 和一些UILabels,并且想要在用户点击单元格中的任何位置时触发特定操作,除了按钮上。我说的对吗??
  • 完全正确。这些单元格具有“漂亮”的背景,并带有标识它们是什么的文本。我希望用户能够点击它们,然后调用 IBAction,但单元格内还有一个特定的按钮应该执行其他操作。而且我还需要知道点击了哪个单元格索引,因为我有一个数组,其中包含需要达到的每个特定索引的特定值。比如用户点击第三个单元格,索引2,我需要有IBAction触发,然后加载mySpecialArray[indexPath.row]

标签: ios xcode swift uitableview


【解决方案1】:

要点击单元格中的任何位置,您无需配置 IBAction。只需使用默认的 UITableViewDelegate 即:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
   let geoSearchWord = "geoSearchWord" + searchQuery[indexPath.row]
    let geoSearchLat = "&geoSearchWordLat=" + (lat == "" ? "33.9700" : lat)
    let geoSearchLon = "&geoSearchWordLon=" + (lat == "" ? "-118.4180" : lon)
    let geoSearchRadius = "&geoSearchWordRad=5mi"
    let twitterURLRequest: String = "https://quiet-cove-5048.herokuapp.com/tw?\(geoSearchWord)\(geoSearchLat)\                      (geoSearchLon)\(geoSearchRadius)"
    alamoRequest(twitterURLRequest)
}

否则,如果您的单元格中有用于特定操作的按钮。您必须为该按钮添加操作并分配一个标签用于识别(点击哪个按钮)。

您可以在 cellForRowAtIndexPath 中添加操作

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "Cell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
    cell.activityLabel?.text = activityNames[indexPath.row]
    cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
    cell.sloganLabel?.text = slogan[indexPath.row]
    cell.myButton.addTarget(self, action: "myAction:", forControlEvents: .TouchUpInside)
    cell.myButton.tag = indexPath.row
    return cell
}

点击单元格内的按钮将执行以下功能:

func myAction(sender:UIButton){
   let selectedActivityName = activityNames[sender.tag]
}

【讨论】:

  • 我有点困惑。我想要一个 IBAction 函数,并且在该函数中,代码将运行。但你是说我应该使用默认的 func tableView(...)?让我们暂时忘记按钮,我将如何分离一个“做事”的函数,以实现模块化,并在单元格点击时调用该函数?
  • 我已经为您提供了上述两种解决方案。您可以使用默认表视图委托或 IBAction 执行操作。在默认委托的情况下,您可以使用 indexPath.row 或在 UIButton 的情况下使用 sender.tag 来分隔您的操作。如果我误解了您的问题,请详细说明一下?
  • 不,是我自己搞糊涂了。你的回答很棒。这是一个包含我所有代码的 jsfiddle。让我们暂时忘记按钮。我将单元格加载到具有适当数据的 func tableView 中。根据被点击的单元格,我想要一个函数,即functionX,这样调用:functionX(indexPath.row)。我是制作应用程序的新手,但到目前为止,我仅有的两种执行操作的方法是 segue 或 IBAction。我认为 func tableView 只是为了加载数据。如何在水龙头上调用一个函数,并将 indexPath.row 作为该函数的参数加载?
  • 已经给出了答案,但仍然让我知道您想在该特定功能中做什么。我会给你完整的代码。
  • 这里是一个jsfiddle的具体功能,和我当前的tableView:jsfiddle.net/rdeuxuwh
【解决方案2】:

这是一个巧妙的解决方案。您可以添加一个 UIControl(例如 UIButton)以在该特殊区域接收触摸。然后在 UITableViewDelegate: tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 中接收事件

【讨论】:

    【解决方案3】:

    如果您想单击单元格中的任何位置,则可以使用其委托方法

    tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
    

    如果你想添加UIControl 和该控件的触摸事件,你需要执行以下操作:

    首先,您需要在cellForRowAtIndexPath 中为您的控件添加目标,如下所示。

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cellIdentifier = "Cell"
            let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TypeOfActivityTableViewCell
            cell.activityLabel?.text = activityNames[indexPath.row]
            cell.bgImage?.image = UIImage(named: activityPic[indexPath.row])
            cell.sloganLabel?.text = slogan[indexPath.row]
            cell.btn.addTarget(self, action: "btnClickAction", forControlEvents: UIControlEvents.TouchDragInside)
            return cell
    }
    

    按钮点击功能:

    func btnClickAction(sender: UIButton)
    {
        var superView = sender.superview
    
        while superView?.isKindOfClass(UITableViewCell) == false
        {
            superView = superView?.superview
        }
    
        var cell = superView as! UITableViewCell
        var indexPath = self.tableView.indexPathForCell(cell)! as NSIndexPath
    }
    

    在上面的代码中你会找到indexPath

    【讨论】:

    • 我已经在使用这个了,但是我想把我的代码分开,以免弄乱。我已经在使用这种方法来构建我的单元格。但是,在此方法之外,我还需要一个 IBAction 来识别点击单元格的索引。
    猜你喜欢
    • 2017-06-04
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多