【问题标题】:Custom Layout UICollectionView自定义布局 UICollectionView
【发布时间】:2018-08-02 05:50:02
【问题描述】:

如何在 Swift 中为集合视图设置水平方向?我需要希望行布局如下图所示。

【问题讨论】:

  • 请检查标签是否有问题,它与ios有关。
  • 好的,现在你已经尝试了什么?分享您的代码并描述什么不符合您的预期..
  • github.com/maximbilan/UICollectionViewHorizontalPaging这个我试过了,但是在我的源代码中合并后它不起作用。
  • 您可以按照 Arek Holko *.com/a/19435898/6356502 的建议尝试以下解决方案
  • @PrateekBhardwaj 请发布您的代码,以便我们可以查看不工作的东西

标签: ios swift3 uicollectionview


【解决方案1】:
@IBOutlet weak var collectionView: UICollectionView!
let cellIdentifier = "cellViewID"
let objects = ["Cat", "Dog", "Fish"]

let images_array = ["break_down_grey", "break_down_grey", "Fish"]

func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! CustomCollectionViewCell

cell.title.text=objects[indexPath.row]
cell.imageView.image = UIImage(named: images_array[indexPath.row])
print(cell)
return cell
}



func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Cell \(indexPath.row) selected")
}

import UIKit

class CustomCollectionViewCell: UICollectionViewCell {



        @IBOutlet  weak var imageView: UIImageView!
        @IBOutlet weak var title: UILabel!



    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}

【讨论】: