在tableview内的collectionview上显示图像

时间:2017-12-21 06:49:52

标签: swift xcode uitableview uicollectionview

我试图在tableview内的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

但如果我运行它,模拟器看起来像enter image description here

如果我选择了其中一个单元格,则除了集合视图上的imageview之外,组件会出现。如果未选择单元格,则模拟器看起来像一个单元格,表示“真的很有趣”#34;有没有人知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

这是关于约束而不是代码。所以,在我正确地给予约束后,它工作正常。