首页 > 加在tableviewcell 的contentview的label刚开始显示不出来是怎么回事?

加在tableviewcell 的contentview的label刚开始显示不出来是怎么回事?

订单管理那个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];
        }
        }

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

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


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


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

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