uicollectionview没有显示细胞迅速

时间:2017-02-17 16:08:34

标签: ios swift uicollectionview

我从网址获取了json字符串的数据。在我的代码中,titles变量是我的url图像的数组。

private let reuseIdentifier = "cell_supporters"

class SupportersCollectionViewController: UICollectionViewController {
    var ids             = [String]()
    var titles          = [String]()

    @IBOutlet var collection_supporters: UICollectionView!

    override func viewDidLoad() {
        super.viewDidLoad()

        collection_supporters.delegate = self

        // Register cell classes
        self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        getSupporters()
    }

    // MARK: UICollectionViewDataSource

//    override func numberOfSections(in collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 1
//    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.titles.count
    }

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

        print("Hello collectionView")

        let url_img = URL(string: self.titles[indexPath.row])!

        print(url_img)
        cell.img_logo.af_setImage(
            withURL: url_img
        )

        return cell
    }

    func getSupporters() {
        RestApiManager.sharedInstance.getSupporters { (json: JSON) in
            if let results = json.array {
                for entry in results {
                    // print(entry["title"])
                    //self.ids.append(entry["id"].string!)
                    self.titles.append(entry["title"].string!)
                }

                print(self.titles.count)

                DispatchQueue.main.async{
                    self.collection_supporters.reloadData()
                }
            }
        }
    }
}

在我的代码中:

print(self.titles.count) // it shows 5

但:

print("Hello collectionView") // not show anything !

4 个答案:

答案 0 :(得分:1)

如果您使用默认的UICollectionViewFlowLayout类进行布局,则可以尝试实现委托方法,该方法返回collectionView中每个项目的大小。如果UICollectionView没有大小信息或者它们为零,则cellForItemAt根本不会调用extension SupportersCollectionViewController: UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: 40, height: 40) // Return any non-zero size here } } dataSource方法。

尝试添加此内容:

NotificationCompat

答案 1 :(得分:0)

ViewDidLoad()方法中添加此行:

self.collectionView!.delegate = self

答案 2 :(得分:0)

将其更改为:

private let reuseIdentifier = "cell_supporters"

class SupportersCollectionViewController: UICollectionViewController {
    var ids             = [String]()
    var titles          = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.register(SupportersCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        getSupporters()
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return self.titles.count
    }

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

        print("Hello collectionView")

        let url_img = URL(string: self.titles[indexPath.row])!

        print(url_img)
        cell.img_logo.af_setImage(
            withURL: url_img
        )

        return cell
    }

    func getSupporters() {
        RestApiManager.sharedInstance.getSupporters { (json: JSON) in
            if let results = json.array {
                for entry in results {
                    // print(entry["title"])
                    //self.ids.append(entry["id"].string!)
                    self.titles.append(entry["title"].string!)
                }

                print(self.titles.count)

                DispatchQueue.main.async{
                    self.collectionView!.reloadData()
                }
            }
        }
    }
}

首先,你不需要这个:

@IBOutlet var collection_supporters: UICollectionView!

因为您的视图控制器是从UICollectionViewController继承的

此外,我将UICollectionViewCell类的寄存器更改为此,以便在创建单元格时不会崩溃:

self.collectionView!.register(SupportersCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

答案 3 :(得分:0)

不要注册细胞类。从viewDidLoad函数中删除以下行:

 self.collectionView!.register(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)