UICollectionView 中的 UILabel 重载 [英] UILabel overloading in UICollectionView

查看:13
本文介绍了UICollectionView 中的 UILabel 重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UICollectionView.每次我使用此代码时,集合视图中的标签都会超载

I am using UICollectionView. in collection view label is overload every time i am using this code

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 90.0,90.0, 21.0)];

    [label setTag : indexPath.row];

    label.text = [NSString stringWithFormat:@"%@",[arrImages objectAtIndex:indexPath.row]];

    label.textColor = [UIColor redColor];

    label.backgroundColor = [UIColor clearColor];

    label.font = [UIFont boldSystemFontOfSize:12];

    label.textColor = [UIColor colorWithRed:46.0/255.0 green:63.0/255.0 blue:81.0/255.0 alpha:1.0];

    [cell.contentView addSubview:label];

return cell;
}

有人帮帮我吗?

推荐答案

单元格将使用 UICollectionView 的回收器出列 - 标签本身留在回收的单元格中,因此无需重新分配它,您只需找到你把它放在哪里:

The cell will get dequeued using UICollectionView's recycler - the label itself stays at the recycled cell, so there's no need in re-allocating it, you just need to find where you placed it:

#define LABEL_TAG 100001

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];

    UILabel *label = (UILabel*)[cell.contentView viewWithTag:LABEL_TAG];

    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 90.0,90.0, 21.0)];
        label.textColor = [UIColor redColor];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor colorWithRed:46.0/255.0 green:63.0/255.0 blue:81.0/255.0 alpha:1.0];
        label.tag = LABEL_TAG;
        [cell.contentView addSubview:label];
    }

    label.text = [NSString stringWithFormat:@"%@",[arrImages objectAtIndex:indexPath.row]];
    return cell;
}

这篇关于UICollectionView 中的 UILabel 重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆