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 · 3034 次点击
    这是一个创建于 2731 天前的主题,其中的信息可能已经有所发展或是发生改变。

    懒加载的时候注册 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   ·   我们的愿景   ·   实用小工具   ·   2920 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 15:23 · PVG 23:23 · LAX 08:23 · JFK 11:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.