菜鸟教程小白 发表于 2022-12-11 16:45:48

ios - 如何覆盖 reloadRowsAtIndexPath?


                                            <p><p>我正在尝试覆盖 tableView 单元格的方法 <code>reloadRowAtIndexPath</code>。 </p>

<p>我想和你重写 <code>cellForRowAtIndexPath</code> 时一样:</p>

<pre><code>func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell
    {
      if(isUserTalking(indexPath))
      {
            return userMessageCell(indexPath)
      }
      else
      {
            if(isNotUserFirstMessage(indexPath))
            {
                return notUserFirstMessageCell(indexPath)
            }
            else
            {
                return notUserNotFirstMessageCell(indexPath)
            }
      }

    }
</code></pre>

<p>这样:</p>

<pre><code>func tableView(tableView: UITableView, reloadRowsAtIndexPaths indexPath: NSIndexPath) -&gt; UITableViewCell
    {
      print(&#34;job&#34;)
            if(isNotUserFirstMessage(indexPath))
            {
                return notUserFirstMessageCell(indexPath, showImage: false)
            }
            else
            {
                return notUserNotFirstMessageCell(indexPath, showImage: false)
            }
    }
</code></pre>

<p>但是当我使用 <code>self.myTable.reloadRowsAtIndexPath</code> 它从不调用覆盖版本。 </p>

<p>你知道怎么解决吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p><code>reloadRowsAtIndexPaths</code> 并不是为了这个。您可能想使用 <code>
tableView(_:willDisplayCell:forRowAtIndexPath:)</code> 代替。例如:</p>

<pre><code>func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    print(&#34;job&#34;)
    if isNotUserFirstMessage(indexPath) {
      configureNotUserFirstMessageCell(cell, showImage: false)
    } else {
      configureNotUserNotFirstMessageCell(cell, showImage: false)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何覆盖 reloadRowsAtIndexPath?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/37886353/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/37886353/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何覆盖 reloadRowsAtIndexPath?