Reputation: 51
How to determine size collection view when I use two different type of cell and the size of one of these cells is always one but other one's size are depend on array and because of that its size is dynamic.
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
}
my ProfileCell number should be one and the number of other cell should be uniqueCountryArray.count. However when I write "return uniqueCountryArray.count + 1" , it gives me error. When I write "return uniqueCountryArray.count" , I missed one array element.
How can I take all of array elements dynamically ? And id array size is 0, I should still show I cell comes from profile Cell.
Upvotes: 1
Views: 135
Reputation: 1391
Change your city = uniqueCountryArray[indexPath.row]
to city = uniqueCountryArray[indexPath.row-1]
like below
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
}
}
And numberOfItemsInSection
will be uniqueCountryArray.count+1
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return uniqueCountryArray.count+1
}
Upvotes: 1