我用 PinterestLayout 创建了一个 CollectionView,所以这是一个具有不同单元格高度的集合视图。这是可行的,但是当我使用 Firestore 将我的代码放入项目时(当我从 URL 获取单元格中的图像时)我遇到了问题,因为在这一行“let photo = post.floverImg”中,我想我必须输入类似的内容单元格“cell.imageView.sd_setImage(with: URL(string: flovers[indexPath.row].floverImg!))”显示图像而不是字符串。
扩展 HomeViewController { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection 部分: Int) -> Int { 返回 flovers.count }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
cell.imageView.sd_setImage(with: URL(string: flovers[indexPath.row].floverImg!))
cell.label.text = flovers[indexPath.row].floverName
// cell.descriptionLabel.text = flovers[indexPath.row].floverName
cell.layer.cornerRadius = 15
return cell
}
}
func collectionView(collectionView: UICollectionView, heightForPhotoAt indexPath: IndexPath, with width: CGFloat) -> CGFloat
{
if let post = posts?[indexPath.item], let photo = post.floverImg {
let boundingRect = CGRect(x: 0, y: 0, width: width, height: CGFloat(MAXFLOAT))
let rect = AVMakeRect(aspectRatio: photo.size(), insideRect: boundingRect)
return rect.size.height
}
return 200
}