美文网首页
iOS 刷新 TableVIew 注意不要创建新的Cell了

iOS 刷新 TableVIew 注意不要创建新的Cell了

作者: 欧大帅Allen | 来源:发表于2022-11-15 15:09 被阅读0次

    reloadData

    Reloads the rows and sections of the table view.

    - (void)reloadData;

    Discussion

    Call this method to reload all the data that’s used to construct the table, including cells, section headers and footers, index arrays, and so on. For efficiency, the table view redisplays only those rows that are visible. It adjusts offsets if the table shrinks as a result of the reload. The table view’s delegate or data source calls this method when it wants the table view to completely reload its data. It shouldn’t be called in the methods that insert or delete rows, especially within an animation block implemented with calls to beginUpdates and endUpdates.

    调用此方法重新加载用于构造表的所有数据,包括单元格、节头和页脚、索引数组等。 为了提高效率,表视图只重新显示那些可见的行。 如果表因重新加载而收缩,它将调整偏移量。 表视图的委托或数据源在希望表视图完全重新加载其数据时调用此方法。 它不应该在插入或删除行的方法中调用,特别是在调用beginUpdates和endppdates实现的动画块中。

    reloadRowsAtIndexPaths:withRowAnimation:

    Reloads the specified rows using the provided animation effect.

    - (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

    Discussion

    Reloading a row causes the table view to ask its data source for a new cell for that row. The table animates that new cell in as it animates the old row out. Call this method if you want to alert the user that the value of a cell is changing. If, however, notifying the user isn’t important — that is, you just want to change the value that a cell is displaying — you can get the cell for a particular row and set its new value.

    When this method is called in an animation block defined by the beginUpdates and endUpdates methods, it behaves similarly to deleteRowsAtIndexPaths:withRowAnimation:. The indexes that UITableView passes to the method are specified in the state of the table view prior to any updates. This happens regardless of ordering of the insertion, deletion, and reloading method calls within the animation block.

    重新加载一行会导致表视图向其数据源请求该行的新单元格。 当表格显示旧行时,它也会显示新单元格。 如果您想提醒用户单元格的值正在更改,则调用此方法。 但是,如果通知用户并不重要—也就是说,您只是想更改单元格显示的值—您可以获取特定行的单元格并设置其新值。
    当这个方法在beginUpdates和endpdates方法定义的动画块中被调用时,它的行为类似于deleteRowsAtIndexPaths:withRowAnimation:。 UITableView传递给方法的索引在任何更新之前都在表视图的状态中指定。 这与动画块中的插入、删除和重新加载方法调用的顺序无关。

    cellForRowAtIndexPath:

    Returns the table cell at the index path you specify.

    - (__kindof UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    Return Value

    The cell object at the corresponding index path. In versions of iOS earlier than iOS 15, this method returns nil if the cell isn’t visible or if indexPath is out of range. In iOS 15 and later, this method returns a non-nil cell if the table view retains a prepared cell at the specified index path, even if the cell isn’t currently visible.

    Discussion

    In iOS 15 and later, the table view retains a prepared cell in the following situations:

    • Cells that the table view prefetches and retains in its cache of prepared cells, but that aren’t visible because the table view hasn’t displayed them yet.
    • Cells that the table view finishes displaying and continues to retain in its cache of prepared cells because they remain near the visible region and might scroll back into view.
    • The cell that contains the first responder.
    • The cell that has focus.
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:2 inSection:0];
        SSPWeekReportErrorQuestionProportionCell *cell = [self.weekReportTableView cellForRowAtIndexPath:indexPath];
        [cell configureForCellModel:self.currentSubjectReportData]; // Cell的高度没有改变
    

    相关文章

      网友评论

          本文标题:iOS 刷新 TableVIew 注意不要创建新的Cell了

          本文链接:https://www.haomeiwen.com/subject/hbmdxdtx.html