的大小。

该方法是UICollectionViewDelegateFlowLayout协议中的一个可选方法,用于设置每个cell的大小。在实现该方法时,需要返回一个CGSize对象,表示对应indexPath的cell的大小。

示例代码如下:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    // 获取屏幕的宽度
    let screenWidth = UIScreen.main.bounds.width
    // 计算每个cell的宽度为屏幕宽度的一半
    let cellWidth = screenWidth / 2
    // 设置cell的高度为固定值
    let cellHeight = 200
    
    return CGSize(width: cellWidth, height: cellHeight)
}

在该示例代码中,我们假设每个cell的高度为200,宽度为屏幕宽度的一半。您可以根据自己的需求来设置cell的大小

func collectionView_ collectionView UICollectionView layout collectionViewLayout UICollectionViewLayout sizeForItemAt indexPath IndexPath - CGSize 获取当对应的Cell

原文地址: http://www.cveoy.top/t/topic/iSMK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录