繁体   English   中英

自定义布局 UICollectionView

[英]Custom Layout UICollectionView

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

参考画面如下:-

@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
    }

}

暂无
暂无

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

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