在容器视图中加载2个以上的集合视图时,iOS App屏幕会冻结

时间:2016-07-28 08:09:27

标签: ios swift uicollectionview

我有一个有4个容器的ViewController。我的意图是使用在单独的ViewController上定义的4个CollectionView填充容器。但问题是,当加载4个集合视图时,我无法对该应用程序执行任何操作。没有按钮也没有任何滚动。它只是冻结 - 没有错误被抛出。当我加载任何2个集合视图时,它工作得很好。知道为什么会这样吗?

这是我的代码:

import UIKit

class HomeViewController: UIViewController  {


    @IBOutlet weak var newProductsContainer: UIView!
    @IBOutlet weak var featuredProductsContainer: UIView!
    @IBOutlet weak var packageListContainer: UIView!
    @IBOutlet weak var productcContainer: UIView!
    @IBOutlet weak var discountContainer: UIView!


    override func viewDidLoad() {
        super.viewDidLoad()
        self.createSlideShow()
        self.setNewProductsContainerContent()
        self.setFeaturedContainerContent()
        self.setPackageListContainerContent()
        print("hello there")
    }

    func setNewProductsContainerContent(){
        let productCollectionVC: ProductCollectionViewController = storyboard?.instantiateViewControllerWithIdentifier("ProductCollection") as! ProductCollectionViewController
        productCollectionVC.collectionType = "new_products"
        self.newProductsContainer.addSubview(productCollectionVC.view)
        self.addChildViewController(productCollectionVC)
    }

    func setFeaturedContainerContent(){
        let productCollectionVC: ProductCollectionViewController = storyboard?.instantiateViewControllerWithIdentifier("ProductCollection") as! ProductCollectionViewController
        productCollectionVC.collectionType = "new_products"
        self.featuredProductsContainer.addSubview(productCollectionVC.view)
        self.addChildViewController(productCollectionVC)
    }

    func setPackageListContainerContent(){
        let productCollectionVC: ProductCollectionViewController = storyboard?.instantiateViewControllerWithIdentifier("ProductCollection") as! ProductCollectionViewController
        productCollectionVC.collectionType = "new_products"
        self.packageListContainer.addSubview(productCollectionVC.view)
        self.addChildViewController(productCollectionVC)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        print("memory warning")
    }


}

你好,打印没有任何问题。

ProductCollectionViewController

import UIKit
import KVNProgress
import ObjectMapper
import Kingfisher

class ProductCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    @IBOutlet weak var productColl: UICollectionView!

    var collectionType: String = "all_products"
    var products: [Product] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        self.productColl.dataSource = self
        self.productColl.delegate = self
        self.initialize()
    }

    func initialize() {

            KVNProgress.show()
            ServiceProvider.getProductsByLimit(0, limit: 10){ (products: [Product]) in
                KVNProgress.dismiss()
                self.products = products
                self.productColl.reloadData()
            }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return products.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("ProCell",forIndexPath:indexPath) as! ProCell
        let product: Product = products[indexPath.row]
        cell.title.text = product.title
        var prices:[Prices] = product.prices
        var price = ""
        if prices.count > 0 {
            price = String(prices[0].retailPrice)
        }
        cell.price.text = price
        var pictures: [Picture] = product.pictures
        print(pictures.count)
        if pictures.count > 0 {
            cell.image.kf_setImageWithURL(NSURL(string: Constants.fineUrl+"mallbackoffice/resources/images/product/general/"+pictures[0].name)!)
        }
        return cell
    }

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        print("clicked")
    }

}

getProductsByLimit 此方法使用Alamofire获取一些没有任何问题的数据。

0 个答案:

没有答案