如何解决如何在collectionview Swift中调整第二个单元格的大小?
我想在我的帖子之间添加原生广告,但问题是“adcell”的第二个单元格使用与“单元格”相同的高度,我该如何更改“asdcell”的高度?
这是我的adcell代码
collectionView.register(UINib(nibName: "AdViewCell",bundle: nil),forCellWithReuseIdentifier: "adcell")
collectionView.delegate = self
collectionView.dataSource = self
view.addSubview(collectionView)
并展示广告
let adcell = collectionView.dequeueReusableCell(withReuseIdentifier: "adcell",for: indexPath) as! AdViewCell
if (indexPath.item % 5 == 1){
//admob code here
return adcell
此代码用于设置帖子高度
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
return CGSize(width: (((collectionView.frame.width) - 40) / 2),height: 500)
} else { //if iPhone
return CGSize(width: view.frame.width-20,height: 500)
}
}
解决方法
如果您知道每五个单元格都会添加一个单元格(顺便说一句,如果您想实现这一点,请将这个 indexPath.item % 5 == 1
更改为 indexPath.item % 5 == 0
),那么在 sizeForItemAt
中,您可以使用cellForItemAt
的相同逻辑:
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.row % 5 == 0{
//return size for addcell ehere
}
if UIScreen.main.bounds.size.height > 960{ //Write iPhone or iPad size. If iPad :
return CGSize(width: (((collectionView.frame.width) - 40) / 2),height: 500)
} else { //if iPhone
return CGSize(width: view.frame.width-20,height: 500)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。