2016-12-19 59 views
3

我目前有一個從Firebase加載的tableView。這個內容包括一張圖片,當用戶滾動時,圖片將會改變,直到它落在最後的圖片上。我會假設這將分配一個圖像到每個單元格,然後才能成功加載每個單元格,但一直沒有能夠提出解決方法。從UITableView中滾動更改的URL加載的圖像

當前的代碼,我要填充的tableView如下:

的TableView

import UIKit 
import FirebaseAuth 
import FirebaseStorage 

class Articles: UITableViewController { 

var vcType:String = "Home" 
var rooms = [Room]() 
var articleCell = ArticlesCell() 
@IBOutlet weak var menuButton: UIBarButtonItem! 
override func viewDidLoad() { 
    super.viewDidLoad() 

    if self.vcType == "Home" 
    { 
     self.rooms += ArticlesManager.sharedClient.rooms 
    } 
    else 
    { 
     if let obj = ArticlesManager.sharedClient.catRooms[self.vcType.lowercased()] //as? [Room] 
     { 
      self.rooms += obj 
     } 
    } 
    self.tableView.reloadData() 

    ArticlesManager.sharedClient.blockValueChangeInRoomArray = { 
     newRoom in 
     if self.vcType == "Home" 
     { 
      self.rooms.append(newRoom) 
      self.rooms.sort(by: { 
       if $0.created_Date == nil 
       { 
        return false 
       } 
       if $1.created_Date == nil 
       { 
        return true 
       } 

       return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending 
      }) 

     } 
     else 
     { 
      if self.vcType.lowercased() == newRoom.category 
      { 
       self.rooms.append(newRoom) 

       self.rooms.sort(by: { 
        if $0.created_Date == nil 
        { 
         return false 
        } 
        if $1.created_Date == nil 
        { 
         return true 
        } 

        return $0.created_Date.compare($1.created_Date) == ComparisonResult.orderedDescending 
       }) 

       self.tableView.reloadData() 

      } 
     } 
    } 

} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return rooms.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    if (indexPath as NSIndexPath).row == 0 { 
     let cell2 = tableView.dequeueReusableCell(withIdentifier: "featured", for: indexPath) as! featuredCell 
     let room = rooms[(indexPath as NSIndexPath).row] 
     cell2.configureCell(room) 
     return cell2 

    } else { 

     let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! ArticlesCell 
     let room = rooms[(indexPath as NSIndexPath).row] 
     cell.configureCell(room) 
     return cell 
} 

數據

import Foundation 
import Firebase 
import FirebaseAuth 


let roomRef = FIRDatabase.database().reference() 

class Data { 

static let dataService = Data() 

fileprivate var _BASE_REF = roomRef 
fileprivate var _ROOM_REF_ = roomRef.child("rooms") 

fileprivate var _BASE_REF2 = roomRef 
fileprivate var _ROOM_REF_2 = roomRef.child("contents") 

var BASE_REF: FIRDatabaseReference { 
    return _BASE_REF 
} 

var ROOM_REF: FIRDatabaseReference { 
    return _ROOM_REF_ 
} 

var storageRef: FIRStorageReference { 
    return FIRStorage.storage().reference() 
} 

var fileURL: String! 

func fetchData(_ callback: @escaping (Room) ->()) { 
    Data.dataService.ROOM_REF.observe(.childAdded, with: { (snapshot) in 

     print("snapshot.value - \(snapshot))") 
     let room = Room(key: snapshot.key, snapshot: snapshot.value as! Dictionary<String, AnyObject>) 
     callback(room) 
    }) 
} 


} 

TableViewCell

import UIKit 
import FirebaseStorage 

class featuredCell: UITableViewCell { 


@IBOutlet weak var featuredImage: UIImageView! 
@IBOutlet weak var featuredTitle: UITextView! 
@IBOutlet weak var featuredAuthor: UITextField! 
@IBOutlet weak var featuredDate: UITextField! 

@IBOutlet weak var featuredContent: UITextView! 
class var defaultHeight: CGFloat { get { return ((UIScreen.main.fixedCoordinateSpace.bounds.height - 64)/5) * 3}} 


func configureCell(_ room: Room) { 
    self.featuredTitle.text = room.title 
    self.featuredDate.text = room.date 
    self.featuredAuthor.text = room.author 
    self.featuredContent.text = room.story 
    if let imageURL = room.thumbnail { 
     if imageURL.hasPrefix("gs://") { 
      FIRStorage.storage().reference(forURL: imageURL).data(withMaxSize: INT64_MAX, completion: { (data, error) in 
       if let error = error { 
        print("Error downloading: \(error)") 
        return 
       } 
       self.featuredImage.image = UIImage.init(data: data!) 
      }) 
     } else if let url = URL(string: imageURL), let data = try? Foundation.Data(contentsOf: url) { 
      self.featuredImage.image = UIImage.init(data: data) 
     } 
    } 

} 

} 

謝謝你的幫忙!

+0

由於您在重複使用單元格,因此featureImage.image仍然可以使用之前的圖像加載。您應該確保configureCell中的if-else條件涵蓋所有情況。例如,如果讓imageURL = room.thumbnail失敗並且沒有其他條件,則featureImage.image仍將加載上一張圖片。 – koropok

回答

0

這是在tableview加載或滾動期間使用異步下載時tableviews的正常行爲。在加載之前,您必須先清除圖像,並將圖像數據保存在緩存中,以便在進行高效的網絡收費時下載一次。如果你不使用緩存,當單元格要重用時,tableview總是「重新下載」圖像。請看例子;

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableViewCell 
    let urle = self.filteredFlats[indexPath.row].flatThumbnailImage?.imageDownloadURL 
    if let url = urle 
    { 
     cell.imgv.image = nil 

     if let imagefromCache = imageCache.object(forKey: url as AnyObject) as? UIImage 
     { 
      cell.imgv.image = imagefromCache 

     } 
     else 
     { 
      cell.imgv.image = nil 
      let urlURL = URL(string: (url)) 

      URLSession.shared.dataTask(with: urlURL!, completionHandler: { (data, response, error) in 
       if error != nil 
       { 
        print(error.debugDescription) 
        return 
       } 
       DispatchQueue.main.async { 
        let imagetoCache = UIImage(data:data!) 
        self.imageCache.setObject(imagetoCache!, forKey: url as AnyObject) 
        cell.imgv.image = imagetoCache 

       } 
      }).resume() 


     } 

    } 

    return cell 



} 

該代碼從我的項目中複製而來。所以我在緩存圖片時已經下載了。另外我不重新下載單元格時重用。只是檢查它是否被緩存。所以你可以使用這個apporach來滿足你的需求。