1

I'm trying to display images on collectionview inside tableview.

I implemented codes like so

 //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

But if I run it simulator looks like this enter image description here

If I selected one of these cell, components come out except imageview on the collectionview. If cell is not being selected, simulator looks like a cell which says "Really fun". Does anyone have any idea how to fix this?

Daibaku
  • 11,416
  • 22
  • 71
  • 108
  • You can take a screenshot of part of your screen by pressing Shift-Command-4 and then dragging a box around the desired area. It looks nicer than a picture. :-) https://support.apple.com/en-us/HT201361 – stone Dec 21 '17 at 06:58
  • cell custom highlight: https://stackoverflow.com/a/31512042/2450755 cell custom selection: https://stackoverflow.com/a/27475774/2450755 – mugx Dec 21 '17 at 07:05
  • Thank you stone. I'll do that next time!! – Daibaku Dec 21 '17 at 07:40
  • Thank you again Andrea!! But is this about background color isn't it? – Daibaku Dec 21 '17 at 07:41
  • Is about having the possibility to control exactly what you want to customize (background, label, anything you may have inside your cell) during the highlight/unhighlight or selection/unselection of the cell. What would you like to customize (or fix) ? From the question above, is not clear for me. – mugx Dec 21 '17 at 07:53
  • I want cell to show components properly. It shows only caption now unless cell is selected. imageview is not shown even after cell is selected. So I want to fix them and want cell to show components from scratch without selecting. – Daibaku Dec 21 '17 at 08:33

1 Answers1

0

It's about constrains not the code. So, after I gave constrains properly it worked fine.

Daibaku
  • 11,416
  • 22
  • 71
  • 108
  • Hi - I am stuck in the same rut as you albeit more than 2 years later. I have to code working fine but cannot get the collection view to show up properly. Do you remember what you did to get the collection view contents to appear properly? – BVB09 Jan 24 '20 at 16:56
  • I don't remember precisely but I think I gave constraints to the collectionvlew like how much px collectionvlew should away from bottom edge of the tablevlew. Plus I had used storyboard so I can't give you code sorry:( I hope this helps. – Daibaku Jan 25 '20 at 02:04