2017-12-23 5 views
0

나는 firebase로 Instagram-clone을 구축하는 데 도움이 필요합니다. 문제는 부엉 게시물 만 있습니다. firebase 저장소에서 이미지를 가져 와서 tableView 셀에 표시 할 수 있습니까? , :(하시기 바랍니다firebase 저장소에서 이미지를 가져 와서 tableView 셀에 표시

수입 UIKit

수입 FirebaseAuth

FirebaseDatabase 수입

클래스 HomeViewController :의 UIViewController, UITableViewDelegate {

,
@IBOutlet weak var tableview: UITableView! 
var posts = [Post]() 
override func viewDidLoad() { 
    super.viewDidLoad() 

    tableview.dataSource = self 
    loadposts() 

// var post = Post(captiontxt: "test", photoUrlString: "urll") 
// print(post.caption) 
// print(post.photoUrl) 

} 

func loadposts() { 
    Database.database().reference().child("posts").observe(.childAdded){ (snapshot: DataSnapshot)in 
     print(Thread.isMainThread) 
      if let dict = snapshot.value as? [String: Any]{ 
      let captiontxt = dict["caption"] as! String 
      let photoUrlString = dict["photoUrl"] as! String 
      let post = Post(captiontxt: captiontxt, photoUrlString: photoUrlString) 
      self.posts.append(post) 
      print(self.posts) 
      self.tableview.reloadData() 
     } 
    } 
} 

@IBAction func logout(_ sender: Any) { 
    do { 
     try Auth.auth().signOut() 
    }catch let logoutErrorr{ 
     print(logoutErrorr) 
    } 
    let storyboard = UIStoryboard(name: "Start", bundle: nil) 
    let signinVC = storyboard.instantiateViewController(withIdentifier: "SigninViewController") 
    self.present(signinVC, animated: true, completion: nil) 


} 

} 확장 HomeViewController : UITableViewDataSource { FUNC에있는 tableView (_있는 tableView : jQuery과, numberOfRowsInSection 부 : INT) -> 지능 { 복귀 posts.count

} 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell 
    cell.captionLabel.text = posts[indexPath.row].caption 
    cell.postimage.image = posts[indexPath.row].photoUrl 
    // print(cell.captionLabel.text) 
    // print(cell.daysLabel.text) 


    return cell 
} 

}

enter code here 

가져 오기 재단 class Post { VAR 설명 : 문자열 var에 photoUrl : 문자열

init(captiontxt: String, photoUrlString: String) { 
    caption = captiontxt 
    photoUrl = photoUrlString 

} 

}

답변

0
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableview.dequeueReusableCell(withIdentifier: "imagecell", for: indexPath) as! PostCellTableViewCell 

    cell.postimage.image = nil 

    cell.tag += 1 
    let tag = cell.tag 

    cell.captionLabel.text = posts[indexPath.row].caption 

    let photoUrl = posts[indexPath.row].photoUrl 

    getImage(url: photoUrl) { photo in 
     if photo != nil { 
      if cell.tag == tag { 
       DispatchQueue.main.async { 
        cell.postimage.image = photo 
       } 
      } 
     } 
    } 

    return cell 
} 

func getImage(url: String, completion: @escaping (UIImage?) ->()) { 
    URLSession.shared.dataTask(with: URL(string: url)!) { data, response, error in 
     if error == nil { 
      completion(UIImage(data: data!)) 
     } else { 
      completion(nil) 
     } 
    }.resume() 
} 
+0

의 작업을하지만, 뭔가 잘못 happing – ghazzway

+0

https://i.stack.imgur.com/XFLCR.png – ghazzway

+0

은 https : //i.stack.imgur.com/yEnzM.png – ghazzway