菜鸟教程小白 发表于 2022-12-12 13:33:25

ios - 在没有动画的情况下将行添加到 tableview 的开头


                                            <p><p>我想用 begin/endUpdates 向 tableview 添加行,以防止在我执行 reloadData 时跳转 tableview</p>

<p>这是我的代码</p>

<pre><code>- (void)updateTableWithNewRowCount:(NSInteger)rowCount
                        andNewData:(NSArray *)newData {
// Save the tableview content offset
CGPoint tableViewOffset = ;

// Turn of animations for the update block
// to get the effect of adding rows on top of TableView
;

;

NSMutableArray *rowsInsertIndexPath = [ init];

int heightForNewRows = 0;

for (NSInteger i = 0; i &lt; rowCount; i++) {

    NSIndexPath *tempIndexPath = ;
    ;
//    atIndex:i];
      ];

    heightForNewRows =
      heightForNewRows + ;
}

[self.messagesTableView insertRowsAtIndexPaths:rowsInsertIndexPath
                              withRowAnimation:UITableViewRowAnimationNone];

tableViewOffset.y += heightForNewRows;

;

;

;
}
</code></pre>

<p>有时(不是每次)我都会收到此错误</p>

<pre><code> invalid number of rows in section 0.
The number of rows contained in an existing section after
the update (2) must be equal to the number of rows contained in that section
before the update (2), plus or minus the number of rows inserted or deleted
from that section (2 inserted, 0 deleted) and plus or minus the number of
rows moved into or out of that section (0 moved in, 0 movedout).
</code></pre>

<p>如何防止这个错误?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果不想要动画直接使用</p>

<pre><code>- (void)updateTableWithNewRowCount:(NSInteger)rowCount
                        andNewData:(NSArray *)newData {
// Save the tableview content offset
CGPoint tableViewOffset = ;

// Turn of animations for the update block
// to get the effect of adding rows on top of TableView

NSMutableArray *rowsInsertIndexPath = [ init];

int heightForNewRows = 0;

for (NSInteger i = 0; i &lt; rowCount; i++) {

    NSIndexPath *tempIndexPath = ;
    ;
//    atIndex:i];
      ];

    heightForNewRows =
      heightForNewRows + ;
}

tableViewOffset.y += heightForNewRows;

;

;
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在没有动画的情况下将行添加到 tableview 的开头,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/27776869/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/27776869/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在没有动画的情况下将行添加到 tableview 的开头