Ich habe eine CollectionView in TableViewController. Die Zellen der Sammlungsansicht dienen zum Anzeigen von Fotos von Benutzern. Was ich jedoch von der Datenbank abrufe, ist der URL-Pfad.Zuweisen von UIImage zu Collection View Cell
ich auch mehr zu manipulieren, auf den Bildern Daten plane, so wählte ich sie in einem Array speichern profileImages
(aber ich bin nicht sicher, ob ich als Array von NSURL verwenden sollte) ..
(I Firebase als Backend Welche lebendigen Streams für die Änderungen ist auch verwenden, das ist, warum ich ein Setup des isIndexValid
Prüfungsteil)
class TableViewController: UITableViewController, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
var profileImages = [NSURL]()
// Database Checks Hits into `retrievePhotos()`
func retrieveUserPhotos(user: AnyObject) {
if let profilePicLink = user["firstImage"]! {
let firstImagePath = profilePicLink as! String
if let fileUrl = NSURL(string: firstImagePath) {
print(fileUrl)
let isIndexValid = profileImages.indices.contains(0)
if (!isIndexValid) {
profileImages.append(fileUrl)
print(profileImages)
} else {
profileImages[0] = fileUrl
}
}
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}
Bisher everyhing schön funktioniert. Ich erhalte jetzt die Bild-URL erfolgreich.
Aber an diesem Punkt wurde ich verwirrt bei der Zuordnung des Bildes zu UIImage in Zelle. (Ich versuchte es mit Heneke, da es einfacher schien).
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
// below returns fatal error `Index out of range`
cell.currentImage.hnk_setImageFromURL(profileImages[indexPath.row])
// Also tried this but it doesn't update the UIImage.
// I guess it's because it's already loaded once without download of the images from the link get completed:
if profileImages.count > 0 {
cell.currentImage.hnk_setImageFromURL(profileImages[indexPath.row])
}
return cell
}
Was ist der richtige Weg, Bilder abzuholen und dem UIImage der Collection View-Zelle zuzuweisen?
Ich habe meine Frage aktualisiert. Wenn ich das Einchecken in 'cellForItemAtIndexPath' durchführe, wird das Bild nie aktualisiert – senty
http://StackOverflow.com/questions/26920632/how-to-asynchroness-load-uitableviewcell-images-so-that-scrolling-doesnt-lag oder https://www.natashatherobot.com/ios-how-to-download-images-asynchron-make-uitableview-scroll-fast/ oder https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction /Intro.html sollte dich auf den Weg bringen – Moonwalkr
Die Antwort war 'collectionView.ReloadData()' danach. Nachdem ich ein bisschen mehr getippt habe, verstehe ich die Natur davon. Danke – senty