首页 > 关于iOS中table view cell重用的问题

关于iOS中table view cell重用的问题

如果我通过registerClass或者registerNib已经注册了cell,那我应该使用dequeueReusableCellWithIdentifier(_:)和dequeueReusableCellWithIdentifier(_:forIndexPath:)方法中的哪一个,还是两个都可以?


使用后者

理由要吗?

请看官方头文件

- (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

前者返回值有个nullable,就是可能为null。

既然你已经registerClass或者registerNib或者用Storyboard设了identifier,那就用第二个吧,不会返回空值。


都可以使用。dequeueReusableCellWithIdentifier适用场合更多


一般用上面的吧,下面的可以针对个别的index设置重用


两个都能用,当你所有的 Cell 都长得一样时用第一个就行了,而在需要长得不一样时用第二个去根据具体的 indexPath 做判断选择显示怎样的 Cell 。

【热门文章】
【热门文章】