Отображение видов на определенные ячейки коллекций? - PullRequest
0 голосов
/ 13 апреля 2019

Я пытаюсь отобразить предварительно закодированные ячейки Page / View на соответствующие ячейки в контроллере представления моей коллекции.

Я создал все страницы без использования раскадровок.

Я создал 3 ячейки, но теперь я пытаюсь выяснить, как поместить каждый вид в соответствующий вид.Я создал массив из 3 моих классов (страниц), но не могу понять, как соединить их с отдельными ячейками в "cellForItemAt". Я попытался поиграть с indexPaths и collectionview.cellForItem (at: ___), но не смогдостичь того, чего я хочу.

Как я могу подключить страницы в моем массиве к нужным ячейкам?

Спасибо

let pages = [AboutPageCell, MainPageCell, InfoPageCell]

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

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

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath)
    return cell
}

1 Ответ

0 голосов
/ 13 апреля 2019

Сначала добавьте это в ViewController

let cellIdentifier = ["AboutPageCell", "MainPageCell", "InfoPageCell"]

Затем в viewDidLoad зарегистрируйте свои ячейки как

tableView.register(AboutPageCell.self, forCellReuseIdentifier: cellIdentifier[0])
tableView.register(MainPageCell.self, forCellReuseIdentifier: cellIdentifier[1])
tableView.register(InfoPageCell.self, forCellReuseIdentifier: cellIdentifier[2])

Ваш cellForItemAt будет

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

        var cellToReturn = collectionView.dequeueReusableCell(withIdentifier: cellIdentifier[indexPath.row])

        switch indexPath.row {
        case 0:
            let aboutPageCell = cellToReturn as! AboutPageCell

            // Configure you propertie here

            cellToReturn = aboutPageCell
        case 1:
            let mainPageCell = cellToReturn as! MainPageCell

            // Configure you propertie here

            cellToReturn = mainPageCell
        case 2:
            let infoCell = cellToReturn as! InfoPageCell

            // Configure you propertie here

            cellToReturn = infoCell
        default:
            break
        }

        return cellToReturn
    }
...