Swift 我在TableViewCells中有CollectionView单元格。我面临的问题是根据不同的模拟调整UIImageView宽度

Swift 我在TableViewCells中有CollectionView单元格。我面临的问题是根据不同的模拟调整UIImageView宽度,swift,uitableview,uicollectionview,Swift,Uitableview,Uicollectionview,我在集合视图布局函数sizeForItemAt中使用了CGSize(宽度:370,高度:240),它不是动态的。宽度:370适用于iPhone 12/Pro,但不适用于其他手机。因此,我得到以下输出:- 我的文件扩展名是: extension EventAttendTableCell : UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource{ func collectionVi

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

我的文件扩展名是:

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”是模型文件中的数据数组。

您可以使用集合视图的宽度减去一些边距作为宽度,而不是静态370。您是否对自定义单元格中的imgatend、labels和others视图的单元格内容设置了一些约束?可能存在导致问题的静态或最小大小。@PtitXav是的,我对单元格项使用了约束。但collectionView单元作为一个整体,单元宽度不等于模拟器宽度,这不是问题吗?@LloydKeijzer我不明白。不是每种设备的利润都不同吗?抱歉,我是iOS开发新手。@chetanjeevsingbains集合视图本身就是一个带框架的视图。该框架具有一个size属性,用于保存集合视图的宽度。在iPhone11上可以是300宽,而在iPad上可以是700宽。例如,您可以使用宽度减去40,使单元格始终为集合视图的全宽减去40,即边距<代码>返回CGSize(宽度:collectionView.frame.size.width-40,高度:240)