我如何从 NSMutableArray 获取图像并发送到我的 collectionView
how i get a image from an NSMutableArray and send to my collectionView
我有这个数组:
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"]]
}
我想获取相应的图片作为单元格,assemble这里:
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
}
单元格名称右侧显示正确但 none 的照片出现
您应该使用数据中提供的图像名称制作一个新的 UIImage
UIImage(named: "imageName.jpg")
然后将其分配给 UIImageView
的图像 属性
您必须实际加载 UIImageView
中的图像,而不仅仅是为其提供图像名称。这假定图像位于主包中(为此您可能不需要 .jpeg 扩展名)。
hotelImageView.image = UIImage(named: topDestinations[indexPath.row]["image"])
我有这个数组:
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"]]
}
我想获取相应的图片作为单元格,assemble这里:
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
}
单元格名称右侧显示正确但 none 的照片出现
您应该使用数据中提供的图像名称制作一个新的 UIImage
UIImage(named: "imageName.jpg")
然后将其分配给 UIImageView
的图像 属性您必须实际加载 UIImageView
中的图像,而不仅仅是为其提供图像名称。这假定图像位于主包中(为此您可能不需要 .jpeg 扩展名)。
hotelImageView.image = UIImage(named: topDestinations[indexPath.row]["image"])