I am trying to display a list of items. My end game is displaying a grid of images from an array of Strings that are URLs and file names on my IOS app.
I have a CollectionView that is displaying a grid of images via an array of String filenames, and that works:
Array:
var tableImages: [String] = ["car.jpg", "bus.jpg", "plane.jpg"]
code that works:
cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
My next step is figuring out the code for displaying a grid of images via an array of String urls. And that is my question. How do I display an image from a url?
Array:
var tableImages: [String] = ["http://www.hotelstmarie.com/wp-content/uploads/2014/12/HSM_Guest-Rooms_1500x750-06-02-2015.jpg", "http://media-cdn.tripadvisor.com/media/photo-s/03/4f/b0/58/aviva-by-kameel.jpg"]
code that works:
?
Notes:
I have checked other stackoverflow questions on the matter but they are from years ago and the move to swift has made their answers out of date.
After I figure out how to display url images I will just make some if statements to recognize url or filename to make it to my endgame, but that is not part of this question.
EDIT 1
Code around my code above:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
cell.labelCell.text = tableData[indexPath.row]
cell.imgCell.image = UIImage(named: tableImages[indexPath.row])
return cell
}
EDIT 2
Using Alamofire:
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: CollViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! CollViewCell
cell.labelCell.text = tableData[indexPath.row]
let imageUrl = NSURL(string: tableImages[indexPath.row])
cell.imgCell.af_setImageWithUrl(imageUrl, placeholderImage: nil, filter: nil, imageTransition: .CrossDissolve(0.2))
return cell
}
Gives this error:
Value of type 'UIImageView' has no member 'af_setImageWithUrl'