【问题标题】:UICollection View custom Cells LayoutUICollectionView 自定义单元格布局
【发布时间】:2017-05-07 11:16:04
【问题描述】:

我正在尝试构建一个演示应用程序(我的技能培训)

该演示应用有一个垂直的集合视图屏幕,其中包含带有图像视图的单元格 和一个带标签的视图

布局原样 - 每个单元格 150 宽和 415 高 2 个单元格,每行垂直

我想让每个单元格在 imageView 内的图像上处于不同的单元格高度,然后我想确保每个单元格都贴在它上面的单元格上

当我尝试这样做时,我设法按比例更改单元格大小,但我无法更改单元格 Y 位置

这是我的全部代码,希望你们明白我的意思,非常感谢提前

import UIKit

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var imagesArray = [#imageLiteral(resourceName: "Forest"),#imageLiteral(resourceName: "Tree") , #imageLiteral(resourceName: "hinna"), #imageLiteral(resourceName: "beach1"),#imageLiteral(resourceName: "work"),#imageLiteral(resourceName: "ya"),#imageLiteral(resourceName: "bird"),#imageLiteral(resourceName: "rose"),#imageLiteral(resourceName: "Tree"),#imageLiteral(resourceName: "Labra") ]

    var labelsArray = ["bahwaijf", "abwohd", "owauhduo", "awodh"]

    @IBOutlet weak var collectionView: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView.delegate = self
        self.collectionView.dataSource = self

       }

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imagesArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CollectionVCe

         cell.cellImg.image = imagesArray[indexPath.row]
        cell.descLabel.text = descArray[indexPath.row]
        cell.itemLabel.text = labelsArray[indexPath.row]

        return cell
    }

}

【问题讨论】:

    标签: ios swift layout uicollectionview


    【解决方案1】:

    您是否尝试过更改单元格插图?您可以在界面生成器上或以编程方式进行。

    界面生成器:

    以编程方式:

    let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout
    
    collectionViewLayout?.sectionInset = ... // some UIEdgeInset
    
    collectionViewLayout?.invalidateLayout()
    

    【讨论】:

    • 我确实试过了,我的问题是我得到的结果是每行有 2 个单元格,但是如果单元格大小更小,下一行不会堆叠到第一行我的意思是说我有 4 个正方形,每行 2 个,第一行有一个左侧正方形,高度为 380,右侧正方形为 415,第二行开始,左侧为 415,即使单元格之间的空间更大,因为第一个单元格为 380而不是 415,它应该从第 2 行的 380 开始,但它没有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-19
    • 2013-03-05
    • 1970-01-01
    • 2021-06-19
    • 2018-08-02
    相关资源
    最近更新 更多