第二个UICollectionView没有重新加载

时间:2018-06-01 04:44:56

标签: ios swift uicollectionview uicollectionviewcell

我试图在同一个UICollectionView中实施两个UIViewController。但是,它出错了,当我运行应用程序只显示一个UICollectionView时,另一个只是保持空白。我尝试了许多方法来解决这个问题,但我总是以失败告终。

import UIKit

class ViewController: UIViewController, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewBanner: UICollectionView!


    var dataSource: [Content] = [Content]()
    var dataBanner: [Banner] = [Banner]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getBanner { (data) in
            DispatchQueue.main.async {
                self.dataBanner = data
                self.collectionViewBanner.reloadData()
            }
        }


        getAudiobooksAPI { (data) in
            DispatchQueue.main.async {
                self.dataSource = data
                self.collectionView.reloadData()
            }
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if self.collectionView.tag == 1 {
            return  self.dataSource.count
        } else {
            return self.dataBanner.count
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if (collectionView == self.collectionView) {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell

            let content = self.dataSource[indexPath.item]

            cell.bookLabel.text = content.descricao
            cell.bookImage.setImage(url: content.urlImagem, placeholder: "")

            return cell

        } else if (collectionView == self.collectionViewBanner) {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCellBanner", for: indexPath) as! CollectionViewCell

            let content = self.dataBanner[indexPath.item]

            cell.bannerImage.setImage(url: content.urlImagem, placeholder: "")

            return cell
        }
        return UICollectionViewCell()
    }

}

extension UIImageView {
    func setImage(url : String, placeholder: String, callback : (() -> Void)? = nil){
        self.image = UIImage(named: "no-photo")

        URLSession.shared.dataTask(with: NSURL(string: url)! as URL, completionHandler: { (data, response, error) -> Void in

            guard error == nil else {
                return
            }
            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data!)
                self.image = image
                if let callback = callback{
                    callback()
                }                    
            })

        }).resume()
    }
}

另一个UICollectionView应该出现在这一个之上。我做错了什么?

App运行图片

enter image description here

[编辑] 我正在调试代码,我在这些行上设置了一个断点。所以,发生的事情是编译器只是读了"如果",没有进入"否则"。

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if self.collectionView.tag == 1 {
        return  self.dataSource.count
    } else {
        return self.dataBanner.count
    }
}

3 个答案:

答案 0 :(得分:1)

请使用我的代码,它将解决您的问题

首先改变

collectionViewBanner.reloadData

中的firstchange

第二次改变

只需检查NumberofiteminSection方法

即可
import UIKit

class ViewController: UIViewController, UICollectionViewDataSource {

    @IBOutlet var collectionView: UICollectionView!
    @IBOutlet weak var collectionViewBanner: UICollectionView!


    var dataSource: [Content] = [Content]()
    var dataBanner: [Banner] = [Banner]()

    override func viewDidLoad() {
        super.viewDidLoad()

        getBanner { (data) in
            DispatchQueue.main.async {
            self.dataBanner = data
            self.collectionViewBanner.reloadData()
            }
        }


        getAudiobooksAPI { (data) in
            DispatchQueue.main.async {
                self.dataSource = data
                self.collectionView.reloadData()
            }
        }
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if (collectionView == self.collectionView) {
        return  self.dataSource.count
        }else{

        return self.dataBanner.count
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        if (collectionView == self.collectionView) {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCell", for: indexPath) as! CollectionViewCell

        let content = self.dataSource[indexPath.item]

        cell.bookLabel.text = content.descricao
        cell.bookImage.setImage(url: content.urlImagem, placeholder: "")

        return cell

        }else if (collectionView == self.collectionViewBanner) {

            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionViewCellBanner", for: indexPath) as! CollectionViewCell

            let content = self.dataBanner[indexPath.item]

            cell.bannerImage.setImage(url: content.urlImagem, placeholder: "")


            return cell
        }
        return UICollectionViewCell()
    }

}

extension UIImageView{
    func setImage(url : String, placeholder: String, callback : (() -> Void)? = nil){
        self.image = UIImage(named: "no-photo")

        URLSession.shared.dataTask(with: NSURL(string: url)! as URL, completionHandler: { (data, response, error) -> Void in

            guard error == nil else{
                return
            }
            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data!)
                self.image = image
                if let callback = callback{
                    callback()
                }


            })

        }).resume()
    }
}

答案 1 :(得分:0)

您忘记重新加载 collectionViewBanner 。刚

collectionViewBanner.reloadData()

答案 2 :(得分:0)

getBanner { (data) in
        DispatchQueue.main.async {
        self.dataBanner = data
        self.collectionViewBanner.reloadData()
        }
    }


    getAudiobooksAPI { (data) in
        DispatchQueue.main.async {
            self.dataSource = data
            self.collectionView.reloadData()
        }
    }


   func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if (collectionView == self.collectionView) { 
       return self.dataSource.count 
    }else if (collectionView == self.collectionViewBanner) { 
       return self.dataBanner.count 
    } else { 
       return nil 
    } 
}