显示从集合视图Swift的网址到单元格的3张图片

时间:2016-05-18 04:35:24

标签: ios swift uicollectionview uicollectionviewcell alamofire

我从API请求了3个图片网址。我想在收集视图上显示已经获取到细胞的网址图像中的3个图像。我是快速编程的新手

这是我的代码

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
     }
}

我的Image View位于名为mainImageView

的mainCollectionViewCell上

2 个答案:

答案 0 :(得分: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
 }
}

希望这会对你有所帮助:)。

答案 1 :(得分: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显示图像