首页 > 解决方案 > 我在 TableViewCells 中有 CollectionView 单元格。我在根据不同的模拟器调整 UIImageView 宽度时遇到问题

问题描述

我在集合视图布局函数 sizeForItemAt 中使用了 CGSize(width: 370, height: 240),它不是动态的。宽度:370 非常适合 iPhone 12/Pro,但不适用于其他手机壳。因此我得到以下输出:- iPhone Mini Simulator

我在文件中的扩展名是:

extension EventAttendTableCell : UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    eventAttendArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    guard let cell = collectionViewEventAttend.dequeueReusableCell(withReuseIdentifier: "EventAttendCollectionCell", for: indexPath) as? EventAttendCollectionCell else {fatalError("Error to create TableViewCell")}
    
    let attendData = eventAttendArray[indexPath.row]
    cell.imgAttend.image = UIImage(named: attendData.imgEventAttendModel)
    cell.lblAttend1.text = attendData.lblEventAttendModelTop
    cell.lblAttend2.text = attendData.lblEventAttendModelMiddle
    cell.lblAttend3.text = attendData.lblEventAttendModelBottom
    
    
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    20
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    20
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: 370, height: 240)
}


}

“eventAttendArray”是模型文件中的数据数组。

标签: swiftuitableviewuicollectionview

解决方案


推荐阅读