菜鸟教程小白 发表于 2022-12-11 19:35:36

ios - 当按钮接收到事件时,UITapGestureRecognizer 到 cellForRowAt


                                            <p><p>我是 Swift 的新手。我被困在这里。我希望当我点击按钮时,单元格会发生变化并显示警告,但我认为我在某个地方错了。请给我解释一下。当我提出问题时,我会考虑很多。 </p>

<p>下面是代码:</p>

<pre><code>@objc func handleTap(_ sender: UITapGestureRecognizer) {
    ManagerProfileTableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;managerProfileCell&#34;) as! ManagerInformationTableCell
    let cellButton = tableView.dequeueReusableCell(withIdentifier: &#34;buttonUpdateProfile&#34;) as! UpdateProfileTableCell

    if indexPath.row == titlesInformationArr.count - 1 {
      cellButton.updateButton.setTitle(&#34;Update&#34;, for: UIControlState.normal)
      cellButton.updateButton.layer.cornerRadius = 5
      let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_: )))
      cellButton.updateButton.isUserInteractionEnabled = true
      cellButton.updateButton.addGestureRecognizer(tap)
      if cell.informationTextField != nil {
            cell.warningImage.isHidden = true
      } else {
            cell.warningImage.isHidden = false
      }
      return cellButton

    } else {
      cell.informationTextField.text = titlesInformationArr.value
      cell.titlesLabel.text = titlesInformationArr.title
      return cell
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您的 <code>cellButton</code> 可能不应该是可重复使用的单元格 - 这就是 <code>cell</code> 的含义 - 而是您添加到出列的可重复使用单元格的 UIButton。如果你像这样添加它,你必须考虑细胞的生命周期。因为它是可重用的,所以即使它以前使用过并且已经有一个按钮,你也会冒着向单元格添加按钮的风险......除非你正在做相反的事情,并使用 <code>prepareForReuse()</code></p>

<p>(当您向上和向下滚动时,当一个单元格离开屏幕时,它会回到一个可供重复使用的单元格池中,并且可以返回给您,处于它消失时的状态,正如您所说的 <code>dequeueReusableCell</code>.)</p>

<p>在这种情况下,您最好在界面构建器中设计原型(prototype)单元格并将按钮添加到其中,然后将 UITableViewCell 子类化并在该子类中为按钮事件 <code>touchUpInside</code> 创建一个 IBAction。当接收到该操作时,单元格可以执行一些操作,其中可能包括调用委托(delegate)方法,其中持有 TableView 的 ViewController 是委托(delegate),以执行与 TableView 相关的操作,例如 <code>reloadData()</code>在你的例子中。 </p>

<p>如果您只想在任何地方检测单元格中的点击,则单元格已经可以做到这一点 - 只需确保 UITableViewDelegate 已连接 - 通常连接到您实现 UITableViewDataSource 的同一位置 - 并实现方法 <code> func tableView(UITableView, didSelectRowAt: IndexPath)</code>.您应该能够在此站点上找到大量使用此方法的示例。</p>

<p>如果您想知道您看到的警告可能意味着什么,您可以编辑答案并包含它吗?</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 当按钮接收到事件时,UITapGestureRecognizer 到 cellForRowAt,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/48199614/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/48199614/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 当按钮接收到事件时,UITapGestureRecognizer 到 cellForRowAt