在表视图单元格内设置集合视图约束的正确方法

2024-01-08

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

以下是集合视图上设置的约束

这是该帖子在 iPhone SE 上的显示效果

Here is how the post is supposed to look on the iPhone 8 Plus enter image description here

Overall post on the storyboard in interface builder. enter image description here

以下是在集合视图内的图像上设置的约束。


Step 1:创建一个 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!

     }


 }

其中集合视图具有以下约束:

Step 2:创建一个 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)

   }

}

Step 3:在控制器中使用 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;
    }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在表视图单元格内设置集合视图约束的正确方法 的相关文章

随机推荐