创建3列UICollectionView

创建3列UICollectionView

我正在尝试创建3列UICollectionView /网格视图,但是尽管尝试了一切,但都没有成功。

我使用的代码如下:

import UIKit

class SearchResultsViewController: UIViewController,UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

// Actual code
@IBOutlet weak var collectionView: UICollectionView!
public var results: [UIImage]!

func loadImages(images: [UIImage]) {
    results = images
}

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.dataSource = self
    collectionView.delegate = self

    self.navigationController?.setNavigationBarHidden(false, animated: true)

    // Disable the margins
    let flow = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
    collectionView.contentOffset = CGPoint.zero
    flow.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
    flow.minimumInteritemSpacing = 0
    flow.minimumLineSpacing = 0
}

// UICollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 64
}

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

    let screenWidth = UIScreen.main.bounds.width
    return CGSize(width: screenWidth/3, height: screenWidth/3)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 0, left: 0, bottom: 5, right: 5)
}

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


    return cell
}


}

我正在寻找类似Instagram之类的照片应用程序的三列布局模拟(请注意,其中64位是虚拟数据,因此我可以测试它是否有效,如果可以使用,它将被实际图像替换)

我没有得到预期的结果,而是这样:
[

最佳答案

像这样使用:

lazy var collectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
    layout.minimumLineSpacing = 10
    layout.minimumInteritemSpacing = 10
    let width = (view.frame.width - 10 * 3) / 2
    layout.itemSize = CGSize(width: width, height: width)
    layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cv.delegate = self
    cv.dataSource = self
    cv.translatesAutoresizingMaskIntoConstraints = false
    cv.showsVerticalScrollIndicator = false
    cv.backgroundColor = .clear
    return cv
}()


另外,您不需要使用这些方法。不再需要。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
}


ios - 无法创建3列UICollectionView-LMLPHP

关于ios - 无法创建3列UICollectionView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53453769/

10-12 03:19