2つの画像を表示したいのですが、別のURLから取得し、別のAPIを2コレクションビューでalamofireを使用して表示します。私はシングルビューコントローラで2コレクションビューを持っています。最初のAPIはパラメータを必要とし、2番目のAPIはパラメータを必要としないので、パラメータを1回宣言します。画像を2コレクションビューに表示するView Controller
は、ここで私はビルドがsucceded
異なるCollectionViewCell.swift上の各画像のコンセントを持っている私のコード
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
}
}
だが、それは
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'UICollectionView must be initialized with a non-nil layout parameter'
を登録するのを忘れXIBモミcollectionviewでレイアウトが流れるように設定されている場合、uは確認することができます。.. Uはuicollectionviewflowlayoutdelegateメソッドを実装していません –