扩展问题来自 New Firebase retrieve data and put on the tableView (Swift) 通过将Firebase代码移动到viewDidLoad,Post的所有文本部分都可以显示当前.但我仍然无法将图像放到tableView中.我检查了图像检索并
New Firebase retrieve data and put on the tableView (Swift)
通过将Firebase代码移动到viewDidLoad,Post的所有文本部分都可以显示当前.但我仍然无法将图像放到tableView中.我检查了图像检索并且它成功了,我想我从Firebase检索的图像不在post数组中.
这是我的修复代码:
import UIKit import Firebase import FirebaseStorage class timelineTableViewController: UITableViewController { var posts = [Post]() var databaseRef: FIRDatabaseReference! var storageRef: FIRStorageReference! override func viewDidLoad() { super.viewDidLoad() databaseRef = FIRDatabase.database().reference() storageRef = FIRStorage.storage().reference() let userPostRef = self.databaseRef.child("posts") userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in if let postAdd = snapshot.value as? NSDictionary{ let url = snapshot.value?["postPhoto"] as! String let userPhotoUrl = snapshot.value?["userPhoto"] as! String let myPost = Post(data: postAdd) FIRStorage.storage().referenceForURL(url).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in let postPhoto = UIImage(data: data!) }) FIRStorage.storage().referenceForURL(userPhotoUrl).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in let userPhoto = UIImage(data: data!) }) self.posts.insert(myPost, atIndex: 0) self.tableView.reloadData() } }) } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return posts.count } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cellIdentifier = "postCell" let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)as! timelineTableViewCell //Dispatch the main thread here dispatch_async(dispatch_get_main_queue()) { cell.usernameLabel.text = self.posts[indexPath.row].username cell.postText.text = self.posts[indexPath.row].postText cell.timeLabel.text = self.posts[indexPath.row].time cell.postPhoto.image = self.posts[indexPath.row].postPhoto cell.userPhoto.image = self.posts[indexPath.row].userPhoto } return cell } }
我的Post结构希望它可能有所帮助!
struct Post { var username: String? var postText: String? var time: String? var userPhoto: UIImage? var postPhoto: UIImage? var url: String? var userPhotoUrl:String? init(data: NSDictionary) { username = data["username"] as? String postText = data["postText"] as? String time = data["time"]as? String userPhoto = data["userPhoto"] as? UIImage postPhoto = data["postPhoto"] as? UIImage url = data["postPhoto"] as? String userPhotoUrl = data["userPhoto"] as? String } }
希望可以得到一些建议!
只需更改以下方法即可func downloadPost(){ let userPostRef = self.databaseRef.child("posts") userPostRef.queryOrderedByChild("time").observeEventType(.ChildAdded, withBlock: {(snapshot) in if let postAdd = snapshot.value as? NSDictionary { print("\(snapshot.value)") let url = snapshot.value?["postPhoto"] as! String let userPhotoUrl = snapshot.value?["userPhoto"] as! String var myPost = Post(data: postAdd) //variable change to "var" because going to modify let url2 = NSURL(string: url) //postPhoto URL let data = NSData(contentsOfURL: url2!) // this URL convert into Data if data != nil { //Some time Data value will be nil so we need to validate such things myPost.postPhoto = UIImage(data: data!) } let url3 = NSURL(string: userPhotoUrl) //userPhoto URL let data2 = NSData(contentsOfURL: url3!) //Convert into data if data2 != nil { //check the data myPost.userPhoto = UIImage(data: data2!) //store in image } self.posts.insert(myPost, atIndex: 0) // then add the "myPost" Variable print(self.posts.count) } self.tableView.reloadData() // finally going to load the collection of data in tableview }) }
它的加载时间很长.因为转换图像并将其存储在主队列中.
另一种方法是存储URL并访问tableview显示屏上的URL.可以在imageview中应用异步加载.