2016-07-23 5 views
0

Das Problem ist Wenn die Anwendung zum ersten Mal geladen wird, laden sich die Bilder bei der Verzögerung der Verbindung in die falsche Position, wenn die Bilder einige Male wechseln bis der Bildlauf endet und das Bild zum rechten Bild zurückgeht. Ich habe keine Ahnung, warum das passiert. Nachdem die Anwendung geladen wurde, nehmen die Bilder die richtige Position ein. Ich benutze Alamofire und Alamofire Image.UICollectionView Bild wird beim Scrollen in die falsche Position geladen Alamofire

Modell Noticia:

import UIKit 
import SwiftyJSON 
import Alamofire 
import AlamofireImage 

class Noticia: NSObject { 
    //MARK: - Properties 
    var name:String = "" 
    var image:String = "" 
    var number:Int = 0 
    let photoCache = AutoPurgingImageCache(
     memoryCapacity: 100 * 1024 * 1024, 
     preferredMemoryUsageAfterPurge: 60 * 1024 * 1024 
    ) 

    override init() { 

    } 

    func setData(obj:JSON)->Noticia{ 
     self.name = obj["titulo"].stringValue 
     self.image = obj["imagen_portada"].stringValue 
     self.number = Int(obj["id"].stringValue)! 
     return self 
    } 

    //MARK: - Image Caching 

    func cacheImage(image: Image, urlString: String) { 
     photoCache.addImage(image, withIdentifier: urlString) 
    } 

    func cachedImage(urlString: String) -> Image? { 
     return photoCache.imageWithIdentifier(urlString) 
    } 

} 

Viewcontroller

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NoticiaCell 
    let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("CellBillboard", forIndexPath: indexPath) as! BillboardCell 
    //Billboard Cell 
    if(indexPath.item == 0){ 
     cell2.contact = noticia[indexPath.item] 
     return cell2 
    }else{ 
     //Square News Cell 
     cell.contact = noticia[indexPath.item] 
     cell.layer.borderColor = ConstantProjectClass.colorBorderWebBizarro.CGColor 
     cell.layer.borderWidth = ConstantProjectClass.noticiasBorderWidth 
     //Layout custom 
     cell.setNeedsLayout() 
     cell.layoutIfNeeded() 
     return cell 
    } 

} 

Noticia Handy

import UIKit 
import Alamofire 
import AlamofireImage 

class NoticiaCell: UICollectionViewCell { 
    //MARK: - Properties 
    @IBOutlet weak var labelView: UILabel! 
    @IBOutlet weak var imageView: UIImageView! 
    @IBOutlet weak var topLabelConstraint: NSLayoutConstraint! 
    @IBOutlet weak var topImageConstraint: NSLayoutConstraint! 
    var imageUrlString:String? 

    var contact:Noticia = Noticia(){ 
     didSet{ 
      self.labelView.text = self.contact.name 

      imageUrlString = self.contact.image 

      self.imageView.image = nil 

      if let imageFromCache = self.contact.cachedImage(imageUrlString!){ 
       self.imageView.image = imageFromCache 
       return 
      } 

      Alamofire.request(.GET, self.contact.image) 
       .responseImage { response in 
        if let image = response.result.value { 
         dispatch_async(dispatch_get_main_queue(), { 
          let imageToCache = image 
          self.imageView.image = imageToCache 
          self.contact.cacheImage(imageToCache, urlString: self.contact.image) 
         }) 
        } 
       } 
      } 
    } 
} 

Jede Hilfe?

+0

Ich würde Ihnen raten, hinteren und unteren Einschränkungen Ausweichen in einer Sammlungsansicht. Verwenden Sie führende und obere Beschränkungen mit einer Breiteneinschränkung, die über einen Auslass verfügt. Stellen Sie die Breite dynamisch ein, geben Sie ein Seitenverhältnis ein oder suchen Sie eine andere Route. [Beispiel] (http://stackoverflow.com/questions/38517114/collection-view-cells-have-incorrect-content-size) – Sethmr

Antwort

0

Nach las einige Post gründete ich die Lösung:

Viewcontroller

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! NoticiaCell 
     let cell2 = collectionView.dequeueReusableCellWithReuseIdentifier("CellBillboard", forIndexPath: indexPath) as! BillboardCell 
     print("entro") 
     //Billboard Cell 
     if(indexPath.item == 0){ 
      cell2.contact = noticia[indexPath.item] 
      return cell2 
     }else{ 
      //Square News Cell 
      cell.labelView.text = noticia[indexPath.item].name 

      imageUrlString = noticia[indexPath.item].image 

      cell.imageView.image = nil 

      if let imageFromCache = noticia[indexPath.item].cachedImage(imageUrlString!){ 
       cell.imageView.image = imageFromCache 

      }else{ 

       Alamofire.request(.GET, noticia[indexPath.item].image) 
        .responseImage { response in 
         if let image = response.result.value { 
          let imageToCache = image 

          if let updatedCell = collectionView.cellForItemAtIndexPath(indexPath) as? NoticiaCell{ 
           updatedCell.imageView.image = imageToCache 
          } 

          self.noticia[indexPath.item].cacheImage(imageToCache, urlString: self.noticia[indexPath.item].image) 

         } 
       } 
      } 

      cell.layer.borderColor = ConstantProjectClass.colorBorderWebBizarro.CGColor 
      cell.layer.borderWidth = ConstantProjectClass.noticiasBorderWidth 
      //Layout custom 
      cell.setNeedsLayout() 
      cell.layoutIfNeeded() 
      return cell 

     } 
    } 

Der Schlüssel ist der folgende Code:

if let updatedCell = collectionView.cellForItemAtIndexPath(indexPath) as? NoticiaCell{ 
    updatedCell.imageView.image = imageToCache 
} 
Verwandte Themen