问题: 请说一下tableview的重用机制
先看个例子
- (UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT - kStatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.showsVerticalScrollIndicator = NO;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_tableView registerClass:[XXXTableViewCell class] forCellReuseIdentifier:@"XXXCellId"];
}
return _tableView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
XXXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XXXCellId"]
cell.delegate = self;
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
return cell;
}
这里是tableview
通常写法, 而其中dequeueReusableCellWithIdentifier:
, 通过指定一个标识
符来获取一个cell
, 就使用到了tableview
的重用机制
。

如图, UITableView正在向上滑动, 其中
- Cell-1 ~ Cell-5 都在屏幕内
- Cell-0 刚滑出屏幕, Cell-0进入重用池
- Cell-6 即将进入出屏幕
因为都是用的同一标识符(id), Cell-6就可以复用Cell-0开辟的内存, 从而达到复用的目的, 其实这就是TableView的重用机制。
网友评论