菜鸟教程小白 发表于 2022-12-11 19:59:16

iOS:在调用 reloadRowsAtIndexPaths 时,didEndDisplayingCell 被调用了两次


                                            <p><p>每当我调用 <code>reloadRowsAtIndexPaths</code> 时; <code>didEndDisplayingCell</code> 方法被调用了两次。为什么会这样?</p>

<p>我已经实现了以下 UITableViewDelegate 和 UITableViewDataSource 的委托(delegate)方法:</p>

<ul>
<li><p><code>- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section</code></p></li>
<li><p><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath</code></p></li>
<li><p><code>- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath</code></p></li>
</ul>

<p>我实现 <code>didEndDisplayingCell</code> 的原因是,我想分离 Firebase 监听器。</p>

<p><strong>这是我在里面实现的</strong> <code>didEndDisplayingCell</code></p>

<pre><code>- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
// detach the listener for the cell at path index indexPath

CustomCell* customCell = (CustomCell*)(cell);
;
}
</code></pre>

<p>每当单元格监听更新的数据时,它都会触发 ViewController 中的一个方法,该方法会重新加载 tableViewCell。以下是我实现该方法的方式:</p>

<pre><code>-(void)refreshCellWithIndexPath:(NSIndexPath*) indexPath andData:(CustomData*)data{

;

;
withRowAnimation:UITableViewRowAnimationFade];
;
}
</code></pre>

<p>此方法执行时,UITableView 的委托(delegate)方法按以下顺序调用:</p>

<ol>
<li><code>numberOfRowsInSection</code></li>
<li><code>cellForRowAtIndexPath</code></li>
<li><code>didEndDisplayingCell</code></li>
<li><code>didEndDisplayingCell</code></li>
</ol>

<p><strong>为什么第二次调用这个 <code>didEndDisplayingCell</code> 方法?</strong>请帮我解决这个问题。 </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>可能的解决方法:</p>

<p>代替</p>

<pre><code>;
withRowAnimation:UITableViewRowAnimationFade];
;
</code></pre>

<p>使用(迅速):</p>

<pre><code>reloadCounter += 1
CATransaction.begin()
tableView.beginUpdates()
CATransaction.setCompletionBlock {
    self.reloadCounter -= 1
}
tableView.reloadRows(at: , with: .none)
tableView.endUpdates()
CATransaction.commit()
</code></pre>

<p>reloadCounter 是一个 ivar。 </p>

<p>在 didEndDisplaying 中:</p>

<pre><code>func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if (reloadCounter == 0) {
      self.output.didEndDisplaying(viewModel: dataProvider.items)
    }
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于iOS:在调用 reloadRowsAtIndexPaths 时,didEndDisplayingCell 被调用了两次,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/44788055/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/44788055/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iOS:在调用 reloadRowsAtIndexPaths 时,didEndDisplayingCell 被调用了两次