Swift 在1视图控制器中向2个集合视图显示图像

Swift 在1视图控制器中向2个集合视图显示图像,swift,uicollectionview,uicollectionviewcell,alamofire,Swift,Uicollectionview,Uicollectionviewcell,Alamofire,我想在2个集合视图中显示从不同url和不同API使用alamofire获得的2个图像。我在单视图控制器中有两个集合视图。第一个API需要参数,而第二个API不需要参数,所以我声明了一次参数 这是我的密码 class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource { var url = [String]() var secondUrl = [String]() let

我想在2个集合视图中显示从不同url和不同API使用alamofire获得的2个图像。我在单视图控制器中有两个集合视图。第一个API需要参数,而第二个API不需要参数,所以我声明了一次参数

这是我的密码

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {

var url = [String]()
var secondUrl = [String]()

let parameters = [

   "data": 1  
]

let collectionViewAIdentifier = "CollectionViewACell"
let collectionViewBIdentifier = "CollectionViewBCell"

@IBOutlet weak var mainCollectionView: UICollectionView!
@IBOutlet weak var secondMainCollectionView: UICollectionView!

let collectionViewA = UICollectionView()
let collectionViewB = UICollectionView()

override func viewDidLoad() {
    super.viewDidLoad()

    collectionViewA.delegate = self
    collectionViewB.delegate = self

    collectionViewA.dataSource = self
    collectionViewB.dataSource = self

    self.view.addSubview(collectionViewA)
    self.view.addSubview(collectionViewB)

    Alamofire.request(.POST, "URL", parameters:  parameters).responseJSON { response in

        if let value = response.result.value {

            let json = JSON(value)

            let data = json["data"].arrayValue

            let status = json["api_status"].intValue

            if status == 1 {

                print(json["api_message"].stringValue)

                for datas in data {
        self.url.append(datas["companies_photo"].stringValue)
                }
            } else {
                print(json["api_message"].stringValue)
            }
                self.mainCollectionView.reloadData()
        }
    }

    Alamofire.request(.POST, "URL").responseJSON { response in

        if let value = response.result.value {

            let json = JSON(value)

            let data = json["data"].arrayValue

            let status = json["api_status"].intValue

            if status == 1 {

                print(json["api_message"].stringValue)

                for datas in data {

                    self.secondUrl.append(datas["image"].stringValue)
                }
            } else {
                print(json["api_message"].stringValue)
            }
            self.secondMainCollectionView.reloadData()
        }
    }
}

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

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

    return secondUrl.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    if collectionView == self.collectionViewA {
    let cellA = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewAIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell

    let imageUrl:NSURL? = NSURL(string: url[indexPath.row])

    if let url = imageUrl {
        cellA.mainImageView.sd_setImageWithURL(url)
    }
    return cellA
}else {
        let cellB = collectionView.dequeueReusableCellWithReuseIdentifier(collectionViewBIdentifier, forIndexPath:  indexPath) as! SecondCollectionViewCell

        let imageUrl:NSURL? = NSURL(string: secondUrl[indexPath.row])

        if let url = imageUrl {
            cellB.secondMainImageView.sd_setImageWithURL(url)
        }

        return cellB
    }
}
我在不同的CollectionViewCell.swift上有每个图像插座

生成成功,但它在调试时打印SIGABRT错误

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'

您尚未实现uicollectionviewflowlayoutdelegate方法。。。SizeFormiteIndex

您尚未实现uicollectionviewflowlayoutdelegate方法。。。SizeFormiteIndex

您忘记注册类

 collectionViewA.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewAIdentifier)



collectionViewB.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewBIdentifier)

你忘了注册上课了

 collectionViewA.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewAIdentifier)



collectionViewB.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewBIdentifier)

是否可以检查xib fir collectionview中的布局是否设置为flow。。您尚未实现uicollectionviewflowlayoutdelegate方法扫描您检查xib fir collectionview中的布局是否设置为flow。。您尚未实现uicollectionviewflowlayoutdelegate方法