【问题标题】:Swift - Looping through array and add to UICollectionViewSwift - 遍历数组并添加到 UICollectionView
【发布时间】:2016-11-10 04:40:56
【问题描述】:

我创建了一个网格 (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)
}
}

【问题讨论】:

    标签: arrays swift uicollectionview


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-11
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-25
      • 2018-04-07
      • 2013-04-21
      相关资源
      最近更新 更多