菜鸟教程小白 发表于 2022-12-11 18:13:42

ios - UITableViewCell 中的多个按钮在 'top-down' UITableView


                                            <p><p>我正在尝试解决当表格 View “自上而下”时如何在 UITableViewCell 中操作按钮 - 例如,每个新行都添加到表格 View 的顶部。我通过以下代码实现了这一点:</p>

<p>我使用以下代码将新项目插入到我的模型数组中,然后插入到 tableview 中:</p>

<pre><code>let newItem = Item(text: inputTextView.text, time: Date())
items.insert(newItem, at: 0) //inserting into the front of model array

tableView.beginUpdates()
let indexPath:IndexPath = IndexPath(row:0, section:0)
tableView.insertRows(at: , with: .fade)
tableView.endUpdates()
</code></pre>

<p>在 cellForRowAt 函数中,我运行以下代码:</p>

<pre><code>let cell = tableView.dequeueReusableCell(withIdentifier: postCellID, for: indexPath) as! NewPostCell
let item = items

cell.postTextLabel.text = text
cell.timeLabel.text = dateFormatter.string(from: time)

cell.selectionStyle = .none

return cell
</code></pre>

<p>我的每个 tableview 单元格中都有三个按钮。</p>

<p>如何连接这些按钮,以便知道从哪个 indexPath 按下了哪个按钮?</p>

<p>问题是,如果我使用 indexPath.row 来标记按钮,那么所有单元格中的按钮都会被标记为 0,因为每个插入都发生在 indexPath.row 第 0 位的表格顶部。</p >

<p>我想用我的模型数组的当前大小来标记按钮,但这也不起作用,因为当重新使用单元格时,它们可以在那时用数组的长度进行标记,这将是错了。</p>

<p>有很多应用程序在表格 View 类型的设置中具有“顶部的最后一个条目”,并且在单元格中带有按钮。所以一定有办法做到这一点。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以将 <code>UIButton</code> 添加到您的 <code>UITableViewCell</code> 并通过标记访问这些 <code>UIButton</code> 并将目标方法添加到这些按钮:</p>

<pre><code>func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {

      // create a new cell if needed or reuse an old one
      let cell:UITableViewCell = self.tableVw.dequeueReusableCell(withIdentifier: &#34;Cell&#34;) as UITableViewCell!

      //Access UIButton
      let button1:UIButton = cell.viewWithTag(10) as! UIButton
      let button2:UIButton = cell.viewWithTag(11) as! UIButton
      let button3:UIButton = cell.viewWithTag(12) as! UIButton

      //Add Action Methods to UIButtons
      button1.addTarget(self, action: #selector(FisrtButtonClick), for: .touchUpInside)
      button2.addTarget(self, action: #selector(SecondButtonClick), for: .touchUpInside)
      button3.addTarget(self, action: #selector(ThirdButtonClick), for: .touchUpInside)

      return cell
    }
</code></pre>

<p>按钮操作</p>

<pre><code>// MARK: - UIButton Methods.
    func FisrtButtonClick(_ sender: Any){
      //Get Button cell position.
      let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
      let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
      if indexPath != nil {
            print(&#34;Cell indexPath: \(indexPath?.row)&#34;)
      }
    }

    func SecondButtonClick(_ sender: Any){

      //Get Button cell position.
      let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
      let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
      if indexPath != nil {
            print(&#34;Cell indexPath: \(indexPath?.row)&#34;)
      }

    }

    func ThirdButtonClick(_ sender: Any){

      //Get Button cell position.
      let ButtonPosition = (sender as AnyObject).convert(CGPoint.zero, to: tableVw)
      let indexPath = tableVw.indexPathForRow(at: ButtonPosition)
      if indexPath != nil {
            print(&#34;Cell indexPath: \(indexPath?.row)&#34;)
      }

    }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UITableViewCell 中的多个按钮在&#39;top-down&#39; UITableView,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/45015157/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/45015157/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UITableViewCell 中的多个按钮在 &#39;top-down&#39; UITableView