ios - 在表格 View 单元格内设置 Collection View 约束的正确方法

标签 ios swift uitableview uicollectionview constraints

我想知道对此设置约束的正确方法。我在表格 View 单元格内有一个 Collection View ,作为在用户发布的图像之间滑动的一种方式。每个帖子(表格 View 单元格)可以包含多个图像(每个图像都发布在 Collection View 中)。我想要一种类似于 instagram 的风格,你可以在其中滑动图像。我遇到的问题是,当我对 Collection View 和 ImageView 设置约束时,它们不会在设备之间改变。似乎唯一的方法是根据用户使用的设备手动更改 Collection View 单元格和 ImageView 的大小。任何实现我想要实现的外观的替代方法也将受到赞赏。

Here are the constraints set on the collection view

这里是collection view的约束设置

enter image description here

这是帖子在 iPhone SE 上的样子

enter image description here

这是帖子在 iPhone 8 Plus 上的显示效果 enter image description here

Interface Builder 中 Storyboard 上的整体帖子。 enter image description here

这是在图像上设置的约束,它位于 Collection View 内。

最佳答案

第 1 步:创建一个包含 Collection View 的 UITableviewCell(就像 InstagramViewCell)。

import UIKit

class InstagramViewCell: UITableViewCell {

   @IBOutlet weak var innerCellView:innerCell!
   @IBOutlet weak var collectionView: UICollectionView!

   var totalElements = 0

   override func awakeFromNib() {

     super.awakeFromNib()

     let flowLayout = UICollectionViewFlowLayout.init()
     flowLayout.itemSize = CGSize.init(width: self.frame.size.width, height: self.frame.size.height)
     flowLayout.scrollDirection = .horizontal
     self.collectionView?.collectionViewLayout = flowLayout

     self.collectionView.delegate = self
     self.collectionView.dataSource = self
     self.collectionView.register(UINib.init(nibName: "ImageCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "ImageCollectionViewCell")

   }

   override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
   }

   func setInformation(_ numberOFCell : Int, _ information : NSDictionary) {
  self.totalElements = numberOFCell
    self.collectionView.reloadData()
   }

}

extension InstagramViewCell:UICollectionViewDelegateFlowLayout,UICollectionViewDataSource {

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      return CGSize.init(width: self.frame.size.width, height: self.frame.size.height)
   }

   func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
       return 0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
       return 0
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
       return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

       return self.totalElements

    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

       let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ImageCollectionViewCell", for: indexPath) as? ImageCollectionViewCell
       cell?.setInformation("image")
       return cell!

     }


 }

InstagramViewCell

其中 Collection View 具有以下约束:

enter image description here

第 2 步:创建一个包含 ImageView 的 UICollectionViewCell(类似于 ImageCollectionViewCell)。

import UIKit

class ImageCollectionViewCell: UICollectionViewCell {

   @IBOutlet weak var imageView: UIImageView!

   override func awakeFromNib() {
      super.awakeFromNib()
    // Initialization code
   }

   func setInformation(_ imageName : String) {

    self.imageView.image = UIImage.init(named: imageName)

   }

}

enter image description here

第 3 步:在您的 Controller 中使用 InstagramViewCell。

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let backButton = UIButton(type: .custom)
        backButton.frame = CGRect(x:0,y:0,width: 45, height:45)
        backButton.setImage(UIImage(named: "back-arrow-white"), for: .normal)
        let backItem = UIBarButtonItem.init(customView: backButton)
        self.navigationItem.leftBarButtonItem = backItem;


        table.delegate = self
        table.dataSource = self
        table.estimatedRowHeight = 100.0
        table.rowHeight = UITableViewAutomaticDimension
        table.register(UINib.init(nibName: "InstagramViewCell", bundle: nil), forCellReuseIdentifier: "InstagramViewCell")

    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)


    }

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


}

extension ViewController:UITableViewDelegate,UITableViewDataSource {

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

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

        let cell = tableView.dequeueReusableCell(withIdentifier: "InstagramViewCell") as? InstagramViewCell
        let information:NSDictionary = [:]
        cell?.setInformation(10, information)
        return cell!
    }

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

}

enter image description here

关于ios - 在表格 View 单元格内设置 Collection View 约束的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47949166/

相关文章:

ios - HTTP 直播是否支持倒带?

swift - 将 uiview Controller 显示为弹出窗口

ios - 如何从 Parse 中删除对象?

ios - TableView 仅显示一个单元格 - Swift 3

ios - 如何使用 iOS 创建一个大的红色 UIButton?

ios - 当单元格不在屏幕上时如何告诉系统不要进行清理

ios - 如何限制我的应用程序仅适用于 iPhone 6 和 6 Plus?

ios - 谷歌地图的自定义标记图标不可点击(使用委托(delegate))

ios - 从一个 UIButton 创建多个 segues 到同一个 View Controller 或另一个

swift - 在自定义 UITableViewCell 中设置切换目标