UIImage 未添加到 UICollectionView 中的所有单元格
UIImage not being added to all cells in UICollectionView
我添加了一个 collection view
并为每个单元格添加了一个 image view
。我能够为每个单元格添加和缩放图像视图,但它们稍微偏离了中心。我将图像视图的中心设置为单元格的中心,但这导致现在只显示一张图像。
代码如下:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.frame
carImageView.center = cell.center
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let columns: CGFloat = 2
let collectionViewWidth = collectionView.bounds.width
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let spaceBetweenCells = flowLayout.minimumInteritemSpacing * (columns - 1)
let adjustedWidth = collectionViewWidth - spaceBetweenCells
let width: CGFloat = floor(adjustedWidth / columns)
let height: CGFloat = width
return CGSize(width: width, height: height)
}
结果如下:
并且视图层次结构显示所有单元格都是使用它们的 UIViews
创建的,而不是 image views
:
删除线 carImageView.center = cell.center
会使所有图像再次出现,但会偏离中心。有谁知道如何解决这个问题?
你可以试试下面的方法吗?
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.bounds
carImageView.center = CGPoint(x: cell.bounds.width/2, y: cell.bounds.height/2)
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)
我添加了一个 collection view
并为每个单元格添加了一个 image view
。我能够为每个单元格添加和缩放图像视图,但它们稍微偏离了中心。我将图像视图的中心设置为单元格的中心,但这导致现在只显示一张图像。
代码如下:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CarCell", for: indexPath)
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.frame
carImageView.center = cell.center
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let columns: CGFloat = 2
let collectionViewWidth = collectionView.bounds.width
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let spaceBetweenCells = flowLayout.minimumInteritemSpacing * (columns - 1)
let adjustedWidth = collectionViewWidth - spaceBetweenCells
let width: CGFloat = floor(adjustedWidth / columns)
let height: CGFloat = width
return CGSize(width: width, height: height)
}
结果如下:
并且视图层次结构显示所有单元格都是使用它们的 UIViews
创建的,而不是 image views
:
删除线 carImageView.center = cell.center
会使所有图像再次出现,但会偏离中心。有谁知道如何解决这个问题?
你可以试试下面的方法吗?
let carImageView = UIImageView(image: UIImage(systemName: "car"))
carImageView.frame = cell.contentView.bounds
carImageView.center = CGPoint(x: cell.bounds.width/2, y: cell.bounds.height/2)
carImageView.contentMode = .scaleAspectFit
cell.addSubview(carImageView)