2016-05-18 72 views
-1

我从API请求3图片url。并且我想要从已获取到Collection View的单元格的url图像中显示3个图像。我是新来的迅速编程从url显示3张图片到Collection View的单元格Swift

这里是我的mainCollectionViewCell代码

class MainCollectionViewController: UICollectionViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 

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

     if let value = response.result.value { 

      let json = JSON(value) 

      let data = json["data"].arrayValue 

      for datas in data { 

       let result = datas["image"].stringValue 

       print(result) 

      } 
     } 
    } 
} 

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

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

     return cell 
    } 
} 

我的图片浏览一个名为mainImageView

+0

这导致将打印图像的URL吗? –

+0

是的,它会打印图片的网址 –

+1

你应该谷歌任何通用教程的Web请求。他们大多数做类似的东西,你想 – Shubhank

回答

0

我解决了它。这里是我的代码

class MainCollectionViewController: UICollectionViewController { 


@IBOutlet var mainCollectionView: UICollectionView! 
var url = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

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

     if let value = response.result.value { 

      let json = JSON(value) 

      let data = json["data"].arrayValue 

      for datas in data { 

       self.url.append(datas["image"].stringValue) 
      } 
      self.mainCollectionView.reloadData() 
     } 
    } 
} 
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

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

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 



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

       if let url = imageUrl { 

        cell.mainImageView.sd_setImageWithURL(url) 

     } 
     return cell 
} 

我使用SDWebImages显示图像

0

喜欢写东西在你的代码下面,

class MainCollectionViewController: UICollectionViewController { 

var myImageArray : NSMutableArray = NSMutableArray() 

override func viewDidLoad() { 
super.viewDidLoad() 

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

    if let value = response.result.value { 

     let json = JSON(value) 

     let data = json["data"].arrayValue 

     for datas in data { 

      let result = datas["image"].stringValue 

      self.myImageArray.addObject(result) 
      print(result) 

     } 
    } 
} 
} 

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

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

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MainCollectionViewCell 

    let url = NSURL(string: self.myImageArray.objectAtIndex(indexPath.row) as String) 
    let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check 
    cell.mainImageView.image = UIImage(data: data!) 

    return cell 
} 
} 

希望这将帮助你:)

+0

你也可以使用像SDWebImage这样的第三方库从URL加载图片。 –

+0

它说'AnyObject'不能转换为'String';你的意思是使用'as!'强迫低调?在线'let url = NSURL' 然后我添加'!'之后。然后生成成功,但它打印SIGABRT错误 –

+0

您的mainImageView是连接或与您的单元格中的ImageView连接?并通过在URL,数据和图像视图中添加断点来检查。还要在单元格中打印Url来检查数组是否存在? –

相关问题