Как я могу управлять двумя разными клетками при подсчете числа предметов - PullRequest
0 голосов
/ 01 апреля 2020

Я хочу создать collectionView с двумя разными ячейками. Первая ячейка должна отображаться один раз, а вторая - столько, сколько массив. Результат должен быть примерно таким, как на картинке в прикрепленной ссылке. Вот также пример кода для лучшего понимания моей проблемы. Спасибо всем, кто мне помогает !!! 100

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


    switch indexpath { // No indexpath in numberofitemsinsection!
    case 0:
        return 1 //display one time the first cell
    default:
        return images.count // display as often as the array is large the second cell
    }

}

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

    switch indexPath.row {
    case 0:
        let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "addImageCell", for: indexPath)
        return cell
    default:
        let cell = imageCollectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! ImageCell

           cell.imageView.image = images[indexPath.row] 

        cell.delegate = self
        cell.selectedAtIndex = indexPath

        return cell
    }
}

Вот коллекцияПросмотр, который я хочу создать

Ответы [ 2 ]

0 голосов
/ 01 апреля 2020

Вы можете достичь этого внутри cellForItemAt, и вам нужно изменить numberOfItemsInSection, как показано ниже:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    // return 1 more than our data array (the extra one will be the "add item" cell)
    return dataSourceArray.count + 1
}

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

    // if indexPath.item is less than data count, return a "Content" cell
    if indexPath.item < dataSourceArray.count {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ContentCell", for: indexPath) as! ContentCell

        // configure your ContentCell: cell. <attribute>

        return cell
    }

    // past the end of the data count, so return an "Add Item" cell
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddItemCell", for: indexPath) as! AddItemCell

    // configure your AddCell: cell. <attribute>

    return cell

}

Для этого вам нужно создать ContentCell и AddItemCell, а также иметь dataSourceArray для хранения всех необходимых вам данных.

0 голосов
/ 01 апреля 2020

Почему бы вам не использовать это?

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1 + theArray.count
}
...