簡體   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