根据数据更改UICollectionViewCell内容和笔尖布局 [英] Change UICollectionViewCell content and nib layout based on data

查看:72
本文介绍了根据数据更改UICollectionViewCell内容和笔尖布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,它显示许多子类化为CardCell的UICollectionViewCells.我将变量"type"传递给- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath中的CardCell,我希望CardCell类能够根据传入的类型加载不同的Nib文件.不同的类型需要具有不同的布局.

I have a UICollectionView that is displaying many UICollectionViewCells that I have subclassed as CardCell. I pass the variable "type" to the CardCell in - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath I want the CardCell class to be able to load a different Nib file depending on what type is passed in. The different types need to have different layouts.

问题是我无法在CardCell.m中找出要更改的位置.我尝试使用- (void)prepareForReuse,但是除非用户滚动,否则不会调用.

The problem is I cannot figure out where to change this in my CardCell.m. I tried using - (void)prepareForReuse but that does not call unless the user is scrolling.

推荐答案

您应该在viewDidLoad中注册所需的每个nib文件,如下所示(将正确的名称替换为nib文件和标识符):

You should register each nib file you need in viewDidLoad, something like this (substituting the correct names for the nib file and the identifier):

[self.collectionView registerNib:[UINib nibWithNibName:@"RDCell" bundle:nil] forCellWithReuseIdentifier:@"FirstType"];

然后,在itemForRowAtIndexPath中,测试类型并返回正确的单元格类型:

Then, in itemForRowAtIndexPath, test for type and return the correct type of cell:

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        if (type = @"firstType") {
            FirstCell *cell = (FirstCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"FirstType" forIndexPath:indexPath];
            return cell;
        }else{
            SecondCell *cell = (SecondCell *) [collectionView dequeueReusableCellWithReuseIdentifier:@"SecondType" forIndexPath:indexPath];
            cell.whatever .....
            return cell;
        }
}

这篇关于根据数据更改UICollectionViewCell内容和笔尖布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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