gpt4 book ai didi

ios - 如何快速以编程方式在 UICollectionView 单元格上添加 UIView

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:52 25 4
gpt4 key购买 nike

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as UICollectionViewCell
var template: UIView
template = UIView.init(frame: CGRectMake(10, 10, 50, 50))
template.backgroundColor = UIColor.redColor()
self.cell.addSubview(template)

return cell
}

我尝试将 uiview 拖到 Storyboard上的 collectionview 单元格中,并将 addSubview 拖到 outlet 中,但它也不起作用。

最佳答案

self.cell 中的“self”意味着它引用了一个类/全局变量,而不是您在方法顶部创建的单元格。尝试将 self.cell.addSubview(template) 更改为 cell.addSubview(template)

还有

var template: UIView
template = UIView.init(frame: CGRectMake(10, 10, 50, 50))

可以简化为:

let template = UIView(frame: CGRectMake(10, 10, 50, 50))

关于ios - 如何快速以编程方式在 UICollectionView 单元格上添加 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35109518/

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