我希望在2个集合视图中显示使用alamofire从不同网址和不同API获取的2个图像。我在单视图控制器中有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上有每个图像插座
构建成功,但它使用debug
打印SIGABRT错误Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
答案 0 :(得分:0)
您尚未实施uicollectionviewflowlayoutdelegate方法... SizeforItemAtINDEX
答案 1 :(得分:0)
您忘记注册课程
collectionViewA.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewAIdentifier)
collectionViewB.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewBIdentifier)