gpt4 book ai didi

ios - 如何根据具有自动调整大小的传入数据在 Collection View 单元格中添加随机内容(UIKit 元素)?

转载 作者:行者123 更新时间:2023-11-28 08:13:41 24 4
gpt4 key购买 nike

在这个函数中,我打开了传入的数据类型,并调用了将 subview 添加到我的单元格的函数。此时我没有数据,我插入了 stub 。

方法不起作用,结果是 textview 之后的空白区域(没有图片),我不知道为什么,我将 subview 添加到我的单元格中,理论上它必须起作用。现在我不使用开关。我的单元格高度为 350,标签高度为 50, TextView 高度为 50,因此对于图像/视频,我保持 250。 请帮我出出主意。

func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionNews",for: indexPath) as! NewsCell

cell.personImage.image = UIImage(named: "welder.png")
cell.textView.layer.cornerRadius = 5
cell.addSubview(addImage(image : UIImage(named : "error.png")!, for: cell, at : indexPath ))

//switch (contentArray[indexPath.row]){
//case let image where image is String :
//cell.addSubview(addImage(image : UIImage(named : "error.png")!, for: cell, at : indexPath ))
//case let video where video is String :
//break
//default:
//break
// }

return cell
}


func addImage(for cell : NewsCell, at index : IndexPath)->UIImageView{
let testImg = UIImage(named: "error.png")

let imageForCell = UIImageView(image: testImg)

imageForCell.autoresizingMask = [UIViewAutoresizing.flexibleWidth , UIViewAutoresizing.flexibleHeight]

imageForCell.frame = CGRect(x: cell.textView.bounds.minX, y: cell.textView.bounds.maxY + 5, width: imageForCell.frame.origin.x, height: imageForCell.frame.origin.y)

imageForCell.layer.cornerRadius = 5
imageForCell.layer.masksToBounds = true

return imageForCell

}
func addVideo () {

}

最佳答案

首先:我看到函数 addImage 没有将 UIImage 声明为参数

第二:您为 imageForCell 设置的 frame 属性错误。你应该做类似的事情:(假设你想要你的图像在 textView 下方)

var newFrame = cell.textView.frame
newFrame.y += 5
imageForCell.frame = newFrame

关于ios - 如何根据具有自动调整大小的传入数据在 Collection View 单元格中添加随机内容(UIKit 元素)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43035953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com