使用两种不同类型的单元格时如何确定集合视图的大小
How to determine size of collection view when using two different type of cell
当我使用两种不同类型的单元格时,如何确定集合视图的大小,其中一个单元格的大小始终是一个,而另一个单元格的大小取决于数组,因此它的大小是动态的。
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,我应该仍然显示我的单元格来自配置文件单元格。
将您的 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
}
当我使用两种不同类型的单元格时,如何确定集合视图的大小,其中一个单元格的大小始终是一个,而另一个单元格的大小取决于数组,因此它的大小是动态的。
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,我应该仍然显示我的单元格来自配置文件单元格。
将您的 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
}