search

Home  >  Q&A  >  body text

ios - 加在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];
        }
        }
PHPzPHPz2888 days ago301

reply all(3)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 17:40:27

    I think you’d better define a separate class for the cells with red dots, and try not to create view code in the controller.

    How to customize tableviewCell, you can refer to this blog: http://www.hcios.com/archives/460

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:40:27

    It is recommended to add this red dot in advance. Then control the display and hiding in cellForRowAtIndexPath.

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:40:27

    Where did you assign the value to result.textLabel.text? Before executing [result.textLabel.text isEqualToString:@"Order Management"], there is no assignment operation seen in the code you gave, so it is speculated that it is caused by the wrong order of assignment and judgment.

    reply
    0
  • Cancelreply