0
나는 자산에서 20 개의 이미지를 표시하기 위해 그리드 (4 x 5)를 만들었습니다. 그리드는 현재 collectionView 내부에 collectionView로 설정되어 있습니다. 각 뷰에는 행당 5 개의 이미지가있는 네 개의 행이 있습니다. 나는 배열을 통해 반복하여 전체 20 개의 이미지로 전체 격자를 채우려고합니다.Swift - 배열을 반복하고 UICollectionView에 추가
4 개의 개별 행이 있기 때문에 전체 격자에 배열을 할당 할 수 없다는 것이 문제입니다. 어떻게 배열과 그리드를 반복 할 수 있으며 그에 따라 각 이미지를 셀에 할당 할 수 있습니까?
그리드 뷰를 하나의 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)
}
}