ios - 加在tableviewcell 的contentview的label刚开始显示不出来是怎么回事?
PHPz
PHPz 2017-04-17 17:38:27
[iOS讨论组]

订单管理那个cell 加了个红色label。
但是一开始显示不出来,滑动tableview,重新刷新一下,才显示
如图:

代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"tableViewCell" ;
    UITableViewCell *result ;
    result = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (result == nil) {
        result = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    else{
        while ([result.contentView.subviews lastObject] != nil) {
            [[result.contentView.subviews lastObject] removeFromSuperview];
        }
    }
if ([result.textLabel.text isEqualToString:@"订单管理"]){
                UILabel *newMsgLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
                newMsgLabel.center = CGPointMake(result.vRightPos - 70, result.vHeight/2);
                newMsgLabel.text = @"1   ";
                [newMsgLabel sizeToFit];
                newMsgLabel.textColor = WHITE_COLOR;
                newMsgLabel.textAlignment = NSTextAlignmentCenter;
                newMsgLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
                newMsgLabel.font = TF_FONT_LEVEL_1;
                newMsgLabel.backgroundColor = RED_COLOR;
                newMsgLabel.layer.cornerRadius = newMsgLabel.vHeight/2;
                newMsgLabel.clipsToBounds = YES;
                [result.contentView addSubview:newMsgLabel];
        }
        }
PHPz
PHPz

学习是最好的投资!

全部回复(3)
PHP中文网

我觉得你最好为带红点的cell单独定义一个类,尽量不要在控制器里面去创建view的代码。

自定义tableviewCell的方法,可以参考下这篇博客:http://www.hcios.com/archives/460

迷茫

建议提前加好这个红点。然后在cellForRowAtIndexPath 控制显示隐藏。

迷茫

你在哪里给result.textLabel.text赋的值?在执行[result.textLabel.text isEqualToString:@"订单管理"]之前,从你给出的代码中并没有看到赋值操作,所以推测是赋值和判断顺序错误引起的。

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

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