我如何从NSMutableArray获取图像并发送到我的collectionView

时间:2015-07-27 14:22:50

标签: ios swift nsmutablearray

我有这个数组:

override func viewDidLoad() {
        super.viewDidLoad()

        topDestinations = [["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"huehueuhe"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"huehueuhe"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"huehueuhe"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"huehueuhe"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"vhuehueuhe"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel2.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"],
            ["image": "hotel4.jpeg","name":"Ibiza"]]
    }

我希望将相应的图片作为单元格并在此汇总:

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

        var hotelImageView : UIImageView = cell.viewWithTag(1) as! UIImageView
        var hotelNameLabel : UILabel = cell.viewWithTag(2) as! UILabel

        hotelImageView.image = topDestinations[indexPath.row]["image"] as? UIImage
        hotelNameLabel.text = topDestinations[indexPath.row]["name"] as? String

        return cell
    }

单元名称的右侧显示正确,但没有任何照片出现

2 个答案:

答案 0 :(得分:0)

您应该使用数据提供的图像名称制作新的UIImage

UIImage(named: "imageName.jpg")

然后将其分配给UIImageView的图像属性

答案 1 :(得分:0)

您必须在UIImageView中实际加载图片,而不仅仅是为其提供图片名称。这假设图像在主束中(您可能不需要.jpeg扩展名)。

hotelImageView.image = UIImage(named: topDestinations[indexPath.row]["image"])