objective-c - 关于iOS中table view cell重用的问题
天蓬老师
天蓬老师 2017-04-17 17:31:15
[iOS讨论组]

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

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(4)
ringa_lee

使用后者

理由要吗?

请看官方头文件

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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号