ios - UICollectionView 中的 UILabel 重载

标签 ios iphone objective-c uicollectionview

我正在使用 UICollectionView .每次我使用此代码时, Collection View 中的标签都会重载

- (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 的回收器出列 - 标签本身保留在回收的单元格中,因此无需重新分配它,您只需要找到放置它的位置:

#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;
}

关于ios - UICollectionView 中的 UILabel 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753707/

相关文章:

objective-c - Objective-C 类的方法存储在哪里以及如何存储?

objective-c - 无法发送草稿中的附件 (Mail.app)

ios - 我如何告诉地址簿选择器要填写哪些字段?

ios - 自 iOS 9.0 起不再有可用位置

ios - TableView 聊天扩展消息

ios - NSURLConnection sendAsynchronousRequest 从不释放内存

ios - 如何在 iOS 的 uitableviewcell 中显示消息和图像

iphone - ASIHttpRequest : requestDidFinishSelector vs. requestDidFailSelector

ios - UITextView 和神秘的缺失空间

ios - CoreData删除多个对象