以编程方式将 UILabel 添加到 CollectionViewCells
Add UILabel to CollectionViewCells programmatically
我的应用程序需要几个 CollectionViews
因此我决定以编程方式创建它们。我向每个单元格添加了一个 UILabel
,但是 UILabel 仅添加到随机 cell
s,而其他单元格不显示任何标签。
如果我上下滚动,textLabels 在某些单元格中随机消失并重新出现在其他单元格中。
这是问题的直观描述
(注意标签在我向下滚动时消失了)
我需要所有单元格来显示文本标签。
这是我使用的代码:
I 视图控制器
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 0 , y: 50 , width: 375 , height: 300)
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: frame , collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: NSStringFromClass(CustomCell.self))
view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 250 }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(CustomCell.self), for: indexPath) as! CustomCell
cell.nameLabel.text = "XYZ"
return cell
}
II 自定义单元格
class CustomCell: UICollectionViewCell {
var nameLabel: UILabel!
override init(frame : CGRect) {
super.init(frame : frame)
nameLabel = UILabel(frame: self.frame)
nameLabel.textAlignment = .left
nameLabel.textColor = .black
contentView.addSubview(nameLabel)
contentView.backgroundColor = .red
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
是什么导致了这种异常行为?我的 collectionView
设置不正确吗?
它看起来像是出列问题,因为它发生在信元出列期间。
您应该在 awakeFromNib 方法中初始化 UILabel - 这样 每次 一个单元格出队时,您将创建您需要的标签。
此外,由于您是以编程方式创建 UILabel,因此您应该看看这个 answer,基本上,当您以编程方式创建 UILabel 时,您应该设置更多的值。
我还建议在创建标签时使用 bringSubViewToFront。
你在这一行中做错了 nameLabel = UILabel(frame: self.frame)
,实际上你给出的位置与集合视图单元格的位置(x 和 y 坐标)相同,这是错误的。你需要给 nameLabel 的位置 x=0 和 y=0,然后它就可以正常工作。定义nameLabel的框架如下:
var nameLabel: UILabel = UILabel(frame: CGRect.zero)
override init(frame : CGRect) {
super.init(frame : frame)
nameLabel.frame.size = CGSize(width: self.frame.width, height: self.frame.height)
}
输出如下:
问题是每个单元格都有不同的 frame.origin
值,因此设置 UILabel(frame : self.frame )
是不正确的。
示例:
let label = UILabel(frame: self.frame)
print(self.frame) // (130.0, 420.0, 50.0, 50.0)
// the origin of the label is now CGPoint(130.0, 420.0) with respect to the
//cell it would be totally out of bounds somewhere down below.
每个 UILabel
的 origin
需要 CGPoint(x :0,y :0)
以便它显示在其各自单元格的正上方。
因此,简单的解决方案是用边界替换框架。
let label = UILabel(frame: self.bounds)
我的应用程序需要几个 CollectionViews
因此我决定以编程方式创建它们。我向每个单元格添加了一个 UILabel
,但是 UILabel 仅添加到随机 cell
s,而其他单元格不显示任何标签。
如果我上下滚动,textLabels 在某些单元格中随机消失并重新出现在其他单元格中。 这是问题的直观描述
(注意标签在我向下滚动时消失了)
我需要所有单元格来显示文本标签。
这是我使用的代码:
I 视图控制器
override func viewDidLoad() {
super.viewDidLoad()
let frame = CGRect(x: 0 , y: 50 , width: 375 , height: 300)
let layout = UICollectionViewFlowLayout()
let collectionView = UICollectionView(frame: frame , collectionViewLayout: layout)
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CustomCell.self, forCellWithReuseIdentifier: NSStringFromClass(CustomCell.self))
view.addSubview(collectionView)
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 250 }
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell( withReuseIdentifier: NSStringFromClass(CustomCell.self), for: indexPath) as! CustomCell
cell.nameLabel.text = "XYZ"
return cell
}
II 自定义单元格
class CustomCell: UICollectionViewCell {
var nameLabel: UILabel!
override init(frame : CGRect) {
super.init(frame : frame)
nameLabel = UILabel(frame: self.frame)
nameLabel.textAlignment = .left
nameLabel.textColor = .black
contentView.addSubview(nameLabel)
contentView.backgroundColor = .red
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
是什么导致了这种异常行为?我的 collectionView
设置不正确吗?
它看起来像是出列问题,因为它发生在信元出列期间。
您应该在 awakeFromNib 方法中初始化 UILabel - 这样 每次 一个单元格出队时,您将创建您需要的标签。
此外,由于您是以编程方式创建 UILabel,因此您应该看看这个 answer,基本上,当您以编程方式创建 UILabel 时,您应该设置更多的值。
我还建议在创建标签时使用 bringSubViewToFront。
你在这一行中做错了 nameLabel = UILabel(frame: self.frame)
,实际上你给出的位置与集合视图单元格的位置(x 和 y 坐标)相同,这是错误的。你需要给 nameLabel 的位置 x=0 和 y=0,然后它就可以正常工作。定义nameLabel的框架如下:
var nameLabel: UILabel = UILabel(frame: CGRect.zero)
override init(frame : CGRect) {
super.init(frame : frame)
nameLabel.frame.size = CGSize(width: self.frame.width, height: self.frame.height)
}
输出如下:
问题是每个单元格都有不同的 frame.origin
值,因此设置 UILabel(frame : self.frame )
是不正确的。
示例:
let label = UILabel(frame: self.frame)
print(self.frame) // (130.0, 420.0, 50.0, 50.0)
// the origin of the label is now CGPoint(130.0, 420.0) with respect to the
//cell it would be totally out of bounds somewhere down below.
每个 UILabel
的 origin
需要 CGPoint(x :0,y :0)
以便它显示在其各自单元格的正上方。
因此,简单的解决方案是用边界替换框架。
let label = UILabel(frame: self.bounds)