Swift - 循环遍历数组并添加到UICollectionView

时间:2016-11-10 04:40:57

标签: arrays swift uicollectionview

我创建了一个网格(4 x 5)来显示来自我的资产的20张图像。网格当前在collectionView中设置为collectionView,每行四行,每行五个图像。我试图通过循环数组填充所有20个图像的完整网格。

问题是我无法将数组分配给完整的网格,因为有四个单独的行。如何遍历我的数组和网格,并相应地将每个图像分配给单元格?

如果只能通过将网格视图设置为一个collectionView来完成,我该怎么做。

class Achievements: UICollectionViewController, UICollectionViewDelegateFlowLayout {

private let cellId = "cellId"

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.white

    collectionView?.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

    collectionView?.register(AchievementCell.self, forCellWithReuseIdentifier: cellId)
}

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

    return cell
}

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

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: view.frame.width, height: 60)
}
}

class AchievementCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

required init?(coder adDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let achievementCollectionView: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

    collectionView.backgroundColor = UIColor.clear
    collectionView.translatesAutoresizingMaskIntoConstraints = false

    return collectionView
}()

private let cellId = "achievementIconId"

func setupViews() {
    backgroundColor = UIColor.clear

    addSubview(achievementCollectionView)

    achievementCollectionView.dataSource = self
    achievementCollectionView.delegate = self

    achievementCollectionView.register(IconCell.self, forCellWithReuseIdentifier: cellId)

    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": achievementCollectionView]))
    addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": achievementCollectionView]))
}

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    return collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath as IndexPath)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 60, height: frame.height)
}
}

这是我对每个单元格的看法。

它目前无法正常工作,因为array.count会破坏我的4 x 5设置。

class IconCell: UICollectionViewCell {

var imageArray: [UIImage] = [
    UIImage(named: "Achievement01")!,
    UIImage(named: "Achievement02")!,
    UIImage(named: "Achievement03")!,
    UIImage(named: "Achievement04")!,
    UIImage(named: "Achievement05")!,
    UIImage(named: "Achievement06")!,
    UIImage(named: "Achievement07")!,
    UIImage(named: "Achievement08")!,
    UIImage(named: "Achievement09")!,
    UIImage(named: "Achievement10")!,
    UIImage(named: "Achievement11")!,
    UIImage(named: "Achievement12")!,
    UIImage(named: "Achievement13")!,
    UIImage(named: "Achievement14")!,
    UIImage(named: "Achievement15")!,
    UIImage(named: "Achievement16")!,
    UIImage(named: "Achievement17")!,
    UIImage(named: "Achievement18")!,
    UIImage(named: "Achievement19")!,
    UIImage(named: "Achievement20")!,
    ]

override init(frame: CGRect) {
    super.init(frame: frame)

    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let imageView: UIImageView = {
    let iv = UIImageView()
    iv.contentMode = .scaleAspectFill
    iv.layer.masksToBounds = true
    return iv
}()

func setupViews() {
    addSubview(imageView)

    imageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width)
}
}

1 个答案:

答案 0 :(得分:0)

我能够找到解决方案。说实话很容易。

class Achievements: UICollectionViewController, UICollectionViewDelegateFlowLayout {

private let cellId = "cellId"

var imageArray: [UIImage] = [
    UIImage(named: "Achievement01")!,
    UIImage(named: "Achievement02")!,
    UIImage(named: "Achievement03")!,
    UIImage(named: "Achievement04")!,
    UIImage(named: "Achievement05")!,
    UIImage(named: "Achievement06")!,
    UIImage(named: "Achievement07")!,
    UIImage(named: "Achievement08")!,
    UIImage(named: "Achievement09")!,
    UIImage(named: "Achievement10")!,
    UIImage(named: "Achievement11")!,
    UIImage(named: "Achievement12")!,
    UIImage(named: "Achievement13")!,
    UIImage(named: "Achievement14")!,
    UIImage(named: "Achievement15")!,
    UIImage(named: "Achievement16")!,
    UIImage(named: "Achievement17")!,
    UIImage(named: "Achievement18")!,
    UIImage(named: "Achievement19")!,
    UIImage(named: "Achievement20")!,
    ]

override func viewDidLoad() {
    super.viewDidLoad()

    collectionView?.backgroundColor = UIColor.white

    collectionView?.contentInset = UIEdgeInsets(top: 10, left: 0, bottom: 0, right: 0)

    collectionView?.register(IconCell.self, forCellWithReuseIdentifier: cellId)
}

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

    cell.imageView.image =  imageArray[indexPath.row]

    return cell
}

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

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

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

// Create achievement icon
class IconCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let imageView: UIImageView = {
    let iv = UIImageView()
    iv.contentMode = .scaleAspectFill
    iv.layer.masksToBounds = true
    return iv
}()

func setupViews() {
    addSubview(imageView)

    imageView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.width)
}
}