2016-11-10 6 views
0

アセットから20枚の画像を表示するためのグリッド(4 x 5)を作成しました。グリッドは現在、collectionView内にcollectionViewとして設定されています.4行には、1行につき5つの画像があります。私はアレイ全体をループすることで、20枚の画像すべてでフルグリッドを塗りつぶしようとしています。Swift - 配列をループしてUICollectionViewに追加

4つの別々の行があるため、配列をフルグリッドに割り当てることができないという問題があります。私の配列とグリッドをループし、それに応じて各画像をセルに割り当てることができますか?

グリッドビューを1つの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) 
} 
} 

答えて

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) 
} 
} 
関連する問題