[英]How use two types of UICollectionViewCell for a single UICollectionView?
I'm looking to add another custom UICollectionVIewCell
to my UICollectionView
. 我想将另一个自定义UICollectionVIewCell
添加到我的UICollectionView
。 So I would now have two custom cells in the same collectionView. 因此,我现在将在同一collectionView中具有两个自定义单元格。 All of the delegate
methods I have put in are for the first collectionViewCell
(collectionVIewCellA). 我输入的所有delegate
方法都用于第一个collectionViewCell
(collectionVIewCellA)。
I've added collectionViewCellB to my storyboard, but am unsure of how to tell the viewController
about this new custom cell and to configure it. 我已经将collectionViewCellB添加到我的情节viewController
,但是不确定如何告诉viewController
这个新的自定义单元并进行配置。 Has anyone done this? 有人这样做吗?
Thank you so much for your help. 非常感谢你的帮助。
You can do this by, 你可以这样做
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if (condition for cellA) {
//dequeue cell with cell A identifier
//configure cell
} else {
//dequeue cell with cell B identifier
//configure cell
}
}
assuming the collectionview cell A and B have different reuse identifiers configured in storyboard. 假设collectionview单元A和B在情节提要中配置了不同的重用标识符。
You can use the same logic in sizeForItemAtIndexPath
delegate method to update height depends on cell 您可以在sizeForItemAtIndexPath
委托方法中使用相同的逻辑来更新高度取决于单元格
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
you set condition in collectionView cellForItemAtIndexPath
Delegate method like this way. 您可以通过这种方式在collectionView cellForItemAtIndexPath
Delegate方法中设置条件。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
// your cell condition here
if (self.isMainMenu) {
MainCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"MainCell" forIndexPath:indexPath];
if(cell!=nil){// cell nil condition
}
// cell coding here
return cell;
}
else{
// second custom cell code here
ItemCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ItemCell" forIndexPath:indexPath];
// cell coding here
return cell;
}
}
increase cell height 增加细胞高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
if(first cell condition)
{
return CGSizeMake((collectionView.frame.size.width/4)-5, (collectionView.frame.size.width/4)-5);// your required height
}
else{
return CGSizeMake((collectionView.frame.size.width/4)-5, (collectionView.frame.size.width/4)-5);// your required height
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.