2016-05-20 4 views
1

Я хочу отобразить 2 изображения, которые я получаю из разных URL-адресов и различных API, используя alamofire в 2-х виде коллекции. У меня есть 2 вида коллекции в одном контроллере. первые API нужны параметры, а второй API не нуждается в параметрах, поэтому я объявляю параметры один раз.Показать изображение для 2 Collection View in 1 View Controller

вот мой код

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' 
+0

Может у проверить, если макет в XIb пихты CollectionView устанавливается течь .. U не реализованы методы uicollectionviewflowlayoutdelegate –

ответ

0

Вы не реализованы методы uicollectionviewflowlayoutdelegate ... SizeforItemAtINDEX

0

Вы забыли зарегистрировать класс

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



collectionViewB.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewBIdentifier) 
Смежные вопросы