2017-12-21 21 views
0

테이블 뷰 내에서 collectionview에 이미지를 표시하려고합니다. collectionview에서 테이블 뷰 내부에 이미지를 표시합니다.

나는 그렇게

//Model 
    import UIKit 
    import Firebase 

    struct Post { 

     var key: String 
     var Date: String 
     var tweetImageUrl = [URL]() 
     var Text: String 

     init(snapshot: DataSnapshot) { 

      self.key = snapshot.key 
      self.Date = (snapshot.value as! NSDictionary)["Date"] as? String ?? "" 
      self.Text = (snapshot.value as! NSDictionary)["Text"] as? String ?? "" 
      if let urls = (snapshot.value as! NSDictionary)["tweetImageUrl"] as? [String:String] { 
       for (_,value) in urls { 
       if let finalUrl = URL(string: value) { 
         tweetImageUrl.append(finalUrl) 
       } 
       } 
      } 
     } 
    } 

//tableviewcell 
import UIKit 

class TimellineTableViewCell: UITableViewCell { 

    @IBOutlet var profileImage: UIImageView! 
    @IBOutlet var tempoName: UILabel! 
    @IBOutlet var dateLabel: UILabel! 
    @IBOutlet var caption: UILabel! 
    @IBOutlet var collectionView: UICollectionView! 

    var post: Post? { 
     didSet { 
      tempoName.text = "channing" 
      dateLabel.text = post?.Date 
      caption.text = post?.Text 
      profileImage.image = #imageLiteral(resourceName: "heart") 
     } 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     collectionView.delegate = self 
     collectionView.dataSource = self 
     profileImage.layer.cornerRadius = 30.0 
     profileImage.layer.masksToBounds = true 
    } 
}//class 

extension TimellineTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource { 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     guard let count = post?.tweetImageUrl.count else { return 0 } 
     return count 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeLineCell", for: indexPath) as! TimelineCollectionViewCell 

     cell.configCell(post: post) 

     return cell 
    } 
}//extension 

//collectionviewcell 
import UIKit 

class TimelineCollectionViewCell: UICollectionViewCell { 


    @IBOutlet var imageView: UIImageView! 


    override func awakeFromNib() { 
     super.awakeFromNib() 
    } 

    func configCell(post: Post?) { 

     guard let imageUrl = post?.tweetImageUrl else { return } 

     for url in imageUrl { 

      URLSession.shared.dataTask(with: url, completionHandler: { (data, res, err) in 

       guard let data = data else { return } 
       let image = UIImage(data: data) 

       DispatchQueue.main.async { 
        self.imageView.image = image 
       } 
      }).resume() 
     } 
    } 
}//class 

//viewcontroller 
import UIKit 
import Firebase 

class TimelineViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    @IBOutlet var TimelinetableView: UITableView! 

    var ref: DatabaseReference! { 
     return Database.database().reference() 
    } 
    let uid = Auth.auth().currentUser?.uid 
    var tweets = [Post]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.TimelinetableView.rowHeight = UITableViewAutomaticDimension 
     self.TimelinetableView.estimatedRowHeight = 300 
     fetchTweets() 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     return 300 
    } 

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

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

     let cell = TimelinetableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TimellineTableViewCell 

     cell.post = tweets[indexPath.row] 

     return cell 
    } 

    func fetchTweets() { 
     ref.child("TWEETS").child(uid!).observe(.value, with: { (snapshot) in 

      var result = [Post]() 
      for post in snapshot.children { 
       let post = Post(snapshot: post as! DataSnapshot) 
       result.append(post) 
      } 
      self.tweets = result.sorted(by: { (p1, p2) -> Bool in 
       p1.Date > p2.Date 
      }) 
      self.TimelinetableView.reloadData()    
     }, withCancel: nil) 

    }//func 

}//class 

같은 코드를 구현하지만 그것을 실행하는 경우 나 구성 요소가 collectionview에 이미지 뷰를 제외하고 나와서, 이러한 세포 중 하나를 선택한 경우 시뮬레이터는이 enter image description here

것 같습니다. 셀을 선택하지 않으면 시뮬레이터는 "정말 재미있는"셀처럼 보입니다. 누구든지이 문제를 해결하는 방법을 알고 있습니까?

+0

Shift-Command-4를 누른 다음 원하는 영역 주위로 상자를 드래그하여 화면의 일부 스크린 샷을 찍을 수 있습니다. 그것은 그림보다 멋지게 보인다. :-) https://support.apple.com/en-us/HT201361 – stone

+0

셀 사용자 지정 강조 표시 : https://stackoverflow.com/a/31512042/2450755 셀 사용자 지정 선택 : https://stackoverflow.com/a/ 27475774/2450755 –

+0

돌 감사합니다. 나는 다음에 그것을 할 것이다!! – Daibaku

답변

0

코드에 제약이 있습니다. 그래서 제약 조건을 적절하게 부여한 후에는 정상적으로 작동했습니다.