이진성
이진성

Reputation: 1

ios(Swift) collectionView don't show up

I'd like to show the CollectionView inside the B ViewController using the A ViewController's button. Image. The information in the image is in the json file. Gets the information and invokes the image in the Asset file. There's no problem getting the data in json. But nothing appears. Using the buttons in the A ViewController, What's the problem? The bottom is my code. Thank you.

A ViewController

 //MARK: 4. @objc Button Action
    @objc func topHand(){
        let cham = ChampViewViewController()
        cham.modalPresentationStyle = .fullScreen
        present(cham, animated: true, completion: nil)
    }

B ViewController

class ChampViewViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource{

    var nameArrayCount = 0
    var nameArray = [String]()
    private var collectionView : UICollectionView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        let indenti = "top"
        let layout = UICollectionViewLayout()
        getJson(line: indenti)
        collectionView = UICollectionView(frame: .zero,collectionViewLayout: layout)
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.register(ChamCellCollectionViewCell.self, forCellWithReuseIdentifier: ChamCellCollectionViewCell.identifier)
        guard let collectionsView = collectionView else { return }
        view.addSubview(collectionsView)
        collectionsView.frame = view.bounds
        
    }
    private func getJson(line:String){
        let cellUrl = Bundle.main.url(forResource: line, withExtension: "json")
        let cellData = NSData(contentsOf: cellUrl!)
        do {
            let modelJson = try JSONSerialization.jsonObject(with: cellData! as Data, options: .allowFragments ) as! NSArray
            var models : [Model] = []
            modelJson.forEach { json in
                guard let dic = json as? [String : AnyObject] else {
                    return
                }
                let newModel = Model(name: dic["이름"] as! String,
                                     line: dic["주라인"] as! String, type:
                                        dic["성향"] as! String,
                                     hp: dic["체력"] as! Int,
                                     hpRe: dic["추가체력"] as! Int,
                                     attackPower: dic["공격력"] as! Double,
                                     attackPowerRe: dic["추가공격력"] as! Double,
                                     attackSpeed: dic["공속"] as! Double,
                                     attackSpeedRe: dic["추가공속"] as! Double,
                                     defensive: dic["방어력"] as! Double,
                                     defensiveRe: dic["추가방어력"] as! Double,
                                     magicDefensive: dic["마저"] as! Double,
                                     magicDefensiveRe: dic["추가마저"] as! Double,
                                     row: dic["row"] as! Int,
                                     column: dic["column"] as! Int)
                                     models.append(newModel)
                                    }
            for data in models {
                let key = data.name
                nameArray.append(key)
            }
            nameArrayCount = nameArray.count
        }catch {
            fatalError()
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return nameArrayCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChamCellCollectionViewCell.identifier, for: indexPath) as? ChamCellCollectionViewCell else {
            fatalError()
        }
        cell.imageConfigure(with: UIImage(named:"가렌"))
            return cell
    }
  
}

B ViewController CollectionViewCell Class

import UIKit

class ChamCellCollectionViewCell : UICollectionViewCell{
    static let identifier = "ChamCellCollectionViewCell"
    
    private let imageView : UIImageView = {
       let image = UIImageView()
        image.contentMode = .scaleAspectFit
        return image
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func imageConfigure(with image : UIImage?) {
        imageView.image = image
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        imageView.frame = contentView.bounds
    }
    override func prepareForReuse() {
        super.prepareForReuse()
        imageView.image = nil
    }
}

Upvotes: 0

Views: 42

Answers (2)

Akos Farkas
Akos Farkas

Reputation: 76

Try with a didSet for your nameArrayCount.

var nameArrayCount = 0{
    didSet{
        collectionView.reloadData()
    }
}

Upvotes: 0

stoikokolev
stoikokolev

Reputation: 517

Add this code to getJson() before the catch block:

DispatchQueue.main.async { [weak self] in
    self?.collectionView?.reloadData()
}

Upvotes: 1

Related Questions