UITableView 两个获得重用cell方法的区别

1,

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.

2,

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered


注释中可以看出的端倪,第一个是老的接口,第二个新一点,在使用结构上:

第一个方法在

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

要这么写

cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kTableviewIdentifier];

if (cell == nil) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:kTableviewIdentifier];

}


第二个方法要这么处理

在定义UITableView位置:

[self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTableviewIdentifier];


然后在

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

就省掉了判断为空,可以直接使用

cell = [tableView dequeueReusableCellWithIdentifier:kTableviewIdentifier forIndexPath:indexPath];

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容