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

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

赤い点のあるセルには別のクラスを定義し、コントローラー内にビュー コードを作成しないようにした方がよいと思います。

tableviewCell をカスタマイズする方法については、このブログを参照してください: http://www.hcios.com/archives/460

いいねを押す +0
迷茫

この赤い点は事前に追加しておくことをお勧めします。次にcellForRowAtIndexPathで表示・非表示を制御します。

いいねを押す +0
迷茫

result.textLabel.text に値を割り当てたのはどこですか? [result.textLabel.text isEqualToString:@"Order Management"]を実行する前のコードでは代入操作が見当たりませんので、代入と判定の順序が間違っていることが原因と推測されます。

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート