[求助]关于自定义 'UITableView', 'UITableViewCell' 复用的问题。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
lisonfan
V2EX    iDev

[求助]关于自定义 'UITableView', 'UITableViewCell' 复用的问题。

  •  1
     
  •   lisonfan 2016-11-01 13:01:25 +08:00 3468 次点击
    这是一个创建于 3275 天前的主题,其中的信息可能已经有所发展或是发生改变。

    懒加载的时候注册 Cell :

    - (UITableView *)homeTableView{ if (!_homeTableView) { _homeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain]; self.homeTableView.delegate = self; self.homeTableView.dataSource = self; [self.homeTableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:_identifier]; [self addSubview:self.homeTableView]; } return _homeTableView; } 

    然后创建 Cell 的时候报错 :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ HomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:_identifier forIndexPath:indexPath]; NSLog(@"%@",cell); NSDictionary * dic = _nowNewsDataArr[indexPath.row]; [cell config:dic]; return cell; } 

    我实在没办法了,搜了好久没搜到

    第 1 条附言    2016-11-01 13:38:22 +08:00
    第 2 条附言    2016-11-01 15:56:28 +08:00
    感谢各位的帮助,问题解决了, GitHub 就删了
    21 条回复    2016-11-09 23:52:39 +08:00
    banxi1988
        1
    banxi1988  
       2016-11-01 13:16:57 +08:00
    一眼看不出错误,最后搞一个 Demo 方便别人帮助你.
    lisonfan
        2
    lisonfan  
    OP
       2016-11-01 13:18:03 +08:00
    @banxi1988 好的,我传个 github
    f19009
        4
    f19009  
       2016-11-01 14:06:18 +08:00
    用 [tableView dequeueReusableCellWithIdentifier:@"identifier"] 这个方法
    lisonfan
        5
    lisonfan  
    OP
       2016-11-01 14:15:04 +08:00
    @f19009

    - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0);
    无限递归,这是为毛。。
    acumen
        6
    acumen  
       2016-11-01 14:33:54 +08:00 via iPhone
    弱问:懒加载里 self.homeTableView 不会无限递归吗?
    CommandZi
        7
    CommandZi  
       2016-11-01 15:06:52 +08:00
    你把懒加载里面的 self.homeTableView 都改成 _homeTableView
    lisonfan
        8
    lisonfan  
    OP
       2016-11-01 15:10:16 +08:00
    @CommandZi 懒加载里面的移出来了,还是不行,无限递归导致内存溢出奔溃
    SeanChense
        9
    SeanChense  
       2016-11-01 15:27:53 +08:00
    刚翻 Stack overflow 就看到楼主的问题
    SeanChense
        10
    SeanChense  
       2016-11-01 15:32:25 +08:00
    楼主在 heightForRowAtIndexPath 里调了 cellForRowAtIndexPath ,然鹅后者又要调前者
    lisonfan
        11
    lisonfan  
    OP
       2016-11-01 15:33:19 +08:00
    @SeanChense 英语渣的一逼就删了
    lisonfan
        12
    lisonfan  
    OP
       2016-11-01 15:35:31 +08:00
    @SeanChense 确实
    难怪死循环,如果想动态返回行高怎么怎么做呢?
    SeanChense
        13
    SeanChense  
       2016-11-01 15:37:15 +08:00
    @lisonfan 这方面的教程很多,可以搜索下
    SeanChense
        14
    SeanChense  
       2016-11-01 15:41:41 +08:00
    楼主的代码比较清奇

    有几个槽点
    1.mj 那个 block 是被 copy 起来的, self 会被持有循环引用
    2.把 cell 绑定的数据换成 Model 吧, dict 太初级了
    3.像 `- (void)initWithHomeTableView;` 这样的 initWithXXX 方法,第一楼主返回的 void 这很诡异,第二 WithXXXX 一般 XXX 是参数签名

    其他没看了
    lisonfan
        15
    lisonfan  
    OP
       2016-11-01 15:44:20 +08:00
    @SeanChense 好的,非常感谢你的提点,不然不知道要被困扰多久
    cookiezby
        16
    cookiezby  
       2016-11-01 15:52:13 +08:00
    提醒下楼主把 gitignore 设置好, Pod 文件夹下的东西一般是不用 commit 的。
    lisonfan
        17
    lisonfan  
    OP
       2016-11-01 15:54:44 +08:00
    @cookiezby 好的,谢谢提醒
    lisonfan
        18
    lisonfan  
    OP
       2016-11-01 16:14:02 +08:00
    @SeanChense
    2. 我还特意把 model 转 arr 发出来的
    3. 方法命名规则不熟悉,继续学习。
    SeanChense
        19
    SeanChense  
       2016-11-01 17:05:39 +08:00
    @lisonfan 没事,我也这样过来的。不断学习。
    Pod 要不要加到 ignore 得看情况,像这样你要发出来给别人看就不能忽略,给别人造成困扰。
    lisonfan
        20
    lisonfan  
    OP
       2016-11-01 17:08:11 +08:00
    @SeanChense 嗯嗯,刚刚把配置改了
    iOran
        21
    iOran  
       2016-11-09 23:52:39 +08:00
    最重要的错误是生成 homeTableView 的时候。本意是写一个 getter 方法来获取 homeTableView ,但你在 getter 方法里面调用 self. homeTableView ,这里有严重的 Bug 。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     3265 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 11:53 PVG 19:53 LAX 04:53 JFK 07:53
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86