简体   繁体   English

集合视图单元格布局

[英]Collection view cell layout

Hi guys I am trying to create a AirBnB app lookalike horizontal scrolling. 大家好,我正在尝试创建一个类似于AirBnB应用的水平滚动。 Everything is going great so far but I am walking into some issues. 到目前为止,一切都进展顺利,但我遇到了一些问题。 I try to make every 3rd item bigger. 我尝试使每第三个项目变大。 Somehow tho hey don't resize. 不知为何不调整大小。 see the screenshot below 见下面的截图

The third item has nothing under it but it doesn't resize. 第三项没有任何内容,但没有调整大小。 to the full height. 达到最大高度 Instead it messes the whole layout. 相反,它弄乱了整个布局。

我得到什么

How it should be is 应该是

在此处输入图片说明

Here is what I do 这是我的工作

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    if indexPath.item % 3 == 0 && indexPath.item != 0 {
        return CGSizeMake(420, 520)
    }

    return CGSizeMake(420, 260)
}

What am I doing wrong here? 我在这里做错了什么?

A cells size is set by the UICollectionViewDelegateFlowLayout method 单元格大小由UICollectionViewDelegateFlowLayout方法设置

optional func collectionView(_ collectionView: UICollectionView,
                      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize

For example: 例如:

func collectionView(_ collectionView: UICollectionView,
                      layout collectionViewLayout: UICollectionViewLayout,
      sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize{

    if indexPath.item % 3 == 0 && indexPath.item != 0 {
        return CGSize(400,1200)
    }
    return CGSize(400,600)
}

I think you are counting your cells wrong if you want it like in the photo you should say: 我想如果您想像照片中那样计数细胞是错误的,您应该说:

if (indexPath.item + 1) % 3 == 0 && indexPath.item != 0 {
        return CGSizeMake(420, 520)
    }

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM