Swift UICollectionView с горизонтальной прокруткой и разметкой каждого раздела слева направо на собственной строке - PullRequest
0 голосов
/ 28 января 2020

Я хотел бы иметь UICollectionView с горизонтальной прокруткой и размещать каждый раздел в отдельной строке слева направо.

В настоящее время мой CollectionView с горизонтальной прокруткой имеет вертикальный макет для каждого раздела.

current layout

В идеале я хотел бы следующий макет с горизонтальной прокруткой:

enter image description here

Так что мой Вопрос заключается в том, как я могу иметь горизонтальную прокрутку на моем UICollectionView, где каждый горизонтальный ряд слева направо расположен в одной строке. (см. вторую диаграмму) Каждый раздел будет занимать разные строки.

Также каждая строка должна одновременно прокручиваться горизонтально (как в электронной таблице).

1 Ответ

2 голосов
/ 28 января 2020

Вы должны объединить 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
    }

}
...