先说一下tableview cell的两个重用方法(一下是iOS的描述)
- (nullable __kindof UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (__kindof UITableViewCell *)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
苹果官方文档解释的已经很清楚了,但是使用的时候你做到不报错了么
1.第二个方法是在5.0及5.0之前的版本上是运行不起来的
2.使用第二个时候会报错误:reason:
通过解释我们可以看出xcode让注册一个nib或者class,方法如下:
- (void)registerNib:(nullable UINib *)nib forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)aClass forHeaderFooterViewReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);
这样当实现cell的dataSource方法时,就不需要写下面这么多了
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell)
{
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
原因是如果所有的重用标识符被注册了,通过dequeueReusableCellWithIdentifier:来返回cell的实例