UITableView常用的刷新方式
// UITableView.h
// 方式1:刷新整個table
- (void)reloadData;
// 方式2:刷新指定的cells
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation;
// 方式3:多個insert/delete批量事務處理
- (void)beginUpdates; // allow multiple insert/delete of rows and sections to be animated simultaneously. Nestable
- (void)endUpdates; // only call insert/delete/reload calls or change the editing state inside an update block. otherwise things like row count, etc. may be invalid.
-
方式1
會刷新整個table
,一般情況下不建議使用 - 當
cell
顯示的數(shù)據(jù)需要發(fā)生變化時嚼吞,通常采用方式2
:只刷新指定的cell(s)
,對table
中的其它cell
不會產(chǎn)生影響。 - 當
insert
/delete
cell
的時候枫绅,一般使用方式3
鲁捏,
單個Cell的局部刷新
案例
我們現(xiàn)在有一個cell芯砸,需要顯示如下3類信息:
- 用戶基本信息:昵稱、性別给梅、頭像假丧、
- 用戶的粉絲數(shù)量
- 用戶的關注數(shù)量
- 以上3類信息的數(shù)據(jù)由服務端提供了3個不同的接口調(diào)用。
要將這個cell的數(shù)據(jù)顯示完全动羽,需要進行3次接口調(diào)用包帚,如果按照方式2
則需要將cell
刷新3次!T讼拧渴邦!如下我將介紹一種cell
局部刷新的方式:
cell局部刷新
// 更新用戶基本信息
[cell updateBasicView];
[tableView beginUpdate];
[tableView endUpdate];
// 更新?用戶粉絲數(shù)量
[cell updateFansView];
[tableView beginUpdate];
[tableView endUpdate];
// 更新?用戶關注數(shù)量
[cell updateFollowView];
[tableView beginUpdate];
[tableView endUpdate];
這樣cell
就做到了局部刷新,是不是很簡單拘哨?谋梭!