【问题标题】:Swift UICollectionView with Horizontal Scrolling and Layout Each Section Left to Right on Own Row具有水平滚动和布局的 Swift UICollectionView 每个部分在自己的行上从左到右
【发布时间】:2020-01-27 22:05:56
【问题描述】:

我希望 UICollectionView 具有水平滚动和布局从左到右在它自己的行上的每个部分。

目前我的 CollectionView with Horizo​​ntal Scrolling 的每个部分都有一个垂直布局。

理想情况下,我想要以下带有水平滚动的布局:

所以我的问题是如何在我的 UICollectionView 上进行水平滚动,其中从左到右的每个水平行都布置在一行中。 (见第二张图)每个部分将占据不同的行。

此外,每一行应同时水平滚动(如电子表格)。

【问题讨论】:

  • 到目前为止你尝试过什么?你能提供给我们任何代码吗?
  • 我尝试了一个解决方案,其中 UITableVIew 中的 UITableViewCell 的每一行都包含一个具有水平滚动的 UICollectionView 但是我希望所有行同时水平滚动而不是单独滚动,这样解决方案将不适用于我目的。

标签: swift xcode uicollectionview uicollectionviewlayout


【解决方案1】:

您必须结合 Collection-view 和 Tableview 才能在 UICollectionView 上进行水平滚动,其中从左到右的每个水平行都布置在一行中。每个部分将占据不同的行。

      class DesignCollectionViewCell: UICollectionViewCell {

        @IBOutlet weak var lbltext: UILabel!
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    }

    class DesignTableViewCell: UITableViewCell {

        @IBOutlet weak var designCollectionView: UICollectionView!
        var numberofScection = Int()

        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
            self.designCollectionView.register(UINib(nibName: "DesignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "DesignCollectionViewCell")
            self.designCollectionView.dataSource = self
            self.designCollectionView.delegate = self
            let layout = UICollectionViewFlowLayout()
            layout.scrollDirection = .horizontal
            self.designCollectionView.collectionViewLayout = layout

        }

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

            // Configure the view for the selected state
        }

    }

    extension DesignTableViewCell: UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{



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

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DesignCollectionViewCell", for: indexPath) as! DesignCollectionViewCell
            cell.lbltext.text = "section \(self.numberofScection) indexpath \(indexPath.row)"
            return cell
        }

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
            return CGSize(width: 170, height: 100.0)
        }
    }

class ViewController: UIViewController {

    @IBOutlet weak var designTableViewCell: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.designTableViewCell.register(UINib(nibName: "DesignTableViewCell", bundle: nil), forCellReuseIdentifier: "DesignTableViewCell")
        self.designTableViewCell.dataSource = self
        self.designTableViewCell.delegate = self

    }


}


extension ViewController: UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "DesignTableViewCell") as! DesignTableViewCell
        cell.designCollectionView.reloadData()
        cell.numberofScection = indexPath.row
        return cell
    }

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

}

【讨论】:

  • 我实际上创建了这个精确的解决方案,但是我希望所有行同时发生所有水平滚动而不是单个行。我很抱歉,因为我的问题中没有。
猜你喜欢
  • 2019-10-22
  • 2013-10-18
  • 2016-08-07
  • 2014-07-16
  • 1970-01-01
  • 2012-11-20
  • 2015-10-27
  • 1970-01-01
  • 2016-11-17
相关资源
最近更新 更多