ios - 加在tableviewcell 的contentview的label刚开始显示不出来是怎么回事?
PHPz
PHPz 2017-04-17 17:38:27
0
3
297

订单管理那个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)
洪涛

我覺得你最好為帶有紅點的cell單獨定義一個類,盡量不要在控制器裡面去創建view的程式碼。

自訂tableviewCell的方法,可以參考下這篇部落格:http://www.hcios.com/archives/460

迷茫

建議提前加好這個紅點。然後在cellForRowAtIndexPath 控制項顯示隱藏。

迷茫

你在哪裡給result.textLabel.text賦的值?在執行[result.textLabel.text isEqualToString:@"訂單管理"]之前,從你給的程式碼中並沒有看到賦值操作,所以推測是賦值和判斷順序錯誤所造成的。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板