UICollectionView 只显示最后一个单元格
UICollectionView only show last cell
我正在以编程方式为菜单执行 collectionView。如此基本,我按下一个按钮,菜单就会从屏幕底部出现。这是有效的,问题是,我正在尝试在我的 collectionview 上设置标签。我设置了所有内容,但是当我 运行 应用程序时,只显示我的最后一个单元格。
所以这是我的 CollectionView:
class SettingsLauncher: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
private var blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
return cv
}()
override init() {
super.init()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: K.CollectioViewCell.cell_identifier_menu)
}
//MARK: - Collection View DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CollectioViewCell.cell_identifier_menu, for: indexPath)
return cell
} func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 60)
}
}
这是我的手机配置:
class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
func setupView() {
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Test"
label.backgroundColor = .red
return label
}()
let labelArray = ["nameLabel" : nameLabel]
class MenuCell: BaseCell {
override func setupView() {
super.setupView()
backgroundColor = .blue
addSubview(nameLabel)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(horizontalConstraint)
NSLayoutConstraint.activate(verticalConstraint)
}
}
视图只能有一个父视图。您的 nameLabel 创建一次,然后添加到每个 MenuCell,这意味着您的标签已从之前的单元格中删除。为每个单元格创建一个 UILabel,您的代码应该可以正常工作,如下所示:
class MenuCell: BaseCell {
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Test"
label.backgroundColor = .red
return label
}()
override func setupView() {
super.setupView()
backgroundColor = .blue
addSubview(nameLabel)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(horizontalConstraint)
NSLayoutConstraint.activate(verticalConstraint)
}
}
我正在以编程方式为菜单执行 collectionView。如此基本,我按下一个按钮,菜单就会从屏幕底部出现。这是有效的,问题是,我正在尝试在我的 collectionview 上设置标签。我设置了所有内容,但是当我 运行 应用程序时,只显示我的最后一个单元格。 所以这是我的 CollectionView:
class SettingsLauncher: NSObject, UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource {
private var blackView = UIView()
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = .white
return cv
}()
override init() {
super.init()
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(MenuCell.self, forCellWithReuseIdentifier: K.CollectioViewCell.cell_identifier_menu)
}
//MARK: - Collection View DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
3
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: K.CollectioViewCell.cell_identifier_menu, for: indexPath)
return cell
} func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: 60)
}
}
这是我的手机配置:
class BaseCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupView()
}
func setupView() {
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Test"
label.backgroundColor = .red
return label
}()
let labelArray = ["nameLabel" : nameLabel]
class MenuCell: BaseCell {
override func setupView() {
super.setupView()
backgroundColor = .blue
addSubview(nameLabel)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(horizontalConstraint)
NSLayoutConstraint.activate(verticalConstraint)
}
}
视图只能有一个父视图。您的 nameLabel 创建一次,然后添加到每个 MenuCell,这意味着您的标签已从之前的单元格中删除。为每个单元格创建一个 UILabel,您的代码应该可以正常工作,如下所示:
class MenuCell: BaseCell {
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Test"
label.backgroundColor = .red
return label
}()
override func setupView() {
super.setupView()
backgroundColor = .blue
addSubview(nameLabel)
let horizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|-[nameLabel]-|", options: [], metrics: nil, views: labelArray)
let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[nameLabel]|", options: [], metrics: nil, views: labelArray)
nameLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(horizontalConstraint)
NSLayoutConstraint.activate(verticalConstraint)
}
}