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

Как определить представление коллекции размеров, когда я использую два разных типа ячеек, и размер одной из этих ячеек всегда один, но размер другой зависит от массива, и из-за этого его размер является динамическим c.

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

        if indexPath.row == 0 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCell", for: indexPath) as! ProfileCollectionViewCell
            cell.bioLabelText.text = bio
            return cell
        }else {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CountryCollectionViewCell

            cell.countryLabel.text = city
            cell.imgView.image = UIImage(named: "lake.jpg")
            cell.makeRounded()
            return cell
        }
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return uniqueCountryArray.count

    }

мой номер ProfileCell должен быть один, а номер другой ячейки должен быть uniqueCountryArray.count. Однако, когда я пишу «return uniqueCountryArray.count + 1», это выдает мне ошибку. Когда я пишу «return uniqueCountryArray.count», я пропускаю один элемент массива.

Как я могу динамически получить все элементы массива? И размер массива id равен 0, я все равно должен показать, что ячейка исходит из профиля Cell.

1 Ответ

1 голос
/ 22 января 2020

Измените city = uniqueCountryArray[indexPath.row] на city = uniqueCountryArray[indexPath.row-1], как показано ниже

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

    if indexPath.row == 0 {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ProfileCell", for: indexPath) as! ProfileCollectionViewCell
        cell.bioLabelText.text = bio
        return cell
    }
    else {

        let city = uniqueCountryArray[indexPath.row-1]

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CountryCollectionViewCell
        cell.countryLabel.text = city
        cell.imgView.image = UIImage(named: "lake.jpg")
        cell.makeRounded()
        return cell
    }
}

И numberOfItemsInSection будет uniqueCountryArray.count+1

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