IOS15给UICollection添加测试数字

IOS15给UICollection添加测试数字,第1张

IOS15给UICollection添加测试数字

如何给collectionView的每一个cell添加一个测试数字,UITableView非常方便,提供了 TitleLabel.text 方法接口,UICollection没有这个方法。
我们自己使用UILabel去添加一个。
核心源码:

 cell.backgroundColor = [UIColor colorWithWhite:234 / 255.0 alpha:0.5];
    NSInteger tag = 100;
    UILabel *lbl = [(UILabel *) cell.contentView viewWithTag:tag];
    if( lbl == nil){
        lbl =  [[UILabel alloc] init];
        lbl.tag = tag;
        [cell.contentView addSubview:lbl];
        lbl.frame = CGRectMake(0, 0, 24, 20);
        lbl.center = CGPointMake(cell.bounds.size.width*0.5,cell.bounds.size.height*0.5);
        lbl.textAlignment = NSTextAlignmentCenter;
    }
    lbl.text = [NSString stringWithFormat:@"%ld",indexPath.item];

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/996932.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存