如何在Swift 4中为1个单元格和第二行以及两个单元格进行自定义布局

时间:2019-01-25 08:21:13

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let sizeW = (collectionView.frame.width - 30) / 2

        let des = (self.feeds[indexPath.row].description ?? "").html2String
        let tit = self.feeds[indexPath.row].title ?? ""
        var basicHeight:CGFloat = 80
        if indexPath.item % 3 == 0{
            let heightDescription = self.heightForView(text:des , font: UIFont.systemFont(ofSize: 15.4), width: collectionView.frame.width - 20)
            let heightTitle = self.heightForView(text:tit , font: UIFont.systemFont(ofSize: 15.4), width: collectionView.frame.width - 20)
            basicHeight = basicHeight + heightDescription + heightTitle

            self.heightArray[indexPath.row] = basicHeight
            firstHeight = 0
            return CGSize(width: collectionView.frame.width - 20, height: basicHeight)
        }else{

            let heightDescription = self.heightForView(text:des , font: UIFont.systemFont(ofSize: 15.4), width: sizeW)
            let heightTitle = self.heightForView(text:tit , font: UIFont.systemFont(ofSize: 15.4), width: sizeW)
            basicHeight = basicHeight + heightDescription + heightTitle
            if self.firstHeight > basicHeight
            {
                basicHeight = firstHeight
            }
            else
            {
                self.firstHeight = basicHeight
            }
            if self.firstHeight > 0{
                let heightofFirstCell = self.heightArray[indexPath.row - 1] ?? 0
                if heightofFirstCell < basicHeight
                {
                    self.heightArray[indexPath.row - 1] = basicHeight
                }
            }
            self.heightArray[indexPath.row] = basicHeight
            return CGSize(width: sizeW, height: basicHeight)
        }
    }

我想交替显示1个单元格和2个单元格,但是在流布局中,它的Y位置不相同。如何使用自定义布局来实现这一目标。对于定制布局,我是全新的。请帮助

0 个答案:

没有答案