[英]UICollectionView dequeues hidden cells when paging is enabled
启用分页后, UICollectionView
仅使3个单元出队,而其余单元则无缘无故地作为隐藏队出队。 也许,这就是UICollectionView
工作方式,但是在我的项目中,我确实需要每次将单元格从cellForItemAt indexPath: IndexPath method
,它实际上会创建自定义UICollectionViewCell
的非隐藏实例。
也许它仅创建3个实例,因为它需要正确管理内存。 但是,在我的项目中,自定义UICollectionViewCell
还包含另一个collectionView
,它由3个自定义collectionView
单元组成。 这些collectionView
单元中还具有tableViews
,这些表中的数据封装在这些单元中。 我的主要问题是:为什么UICollectionView
在我的情况下仅创建3个实例,我该怎么做才能避免这种情况?
我实际项目中的层次结构如下所示: UICollectionView
> UICollectionView
> 3个自定义UICollectionViewCell
>每个UICollectionViewCell
包含一个tableView
>每个tableView
包含一个特定的自定义TableViewCell
。
这是我写的完整代码,作为实际项目中运行情况的示例(这不是实际项目,但行为相同):
ViewController
:
class ViewController: UIViewController {
let cellId = "uniqueCellId"
let sampleWords: [String] = ["one", "two", "three", "four", "five", "six"]
let colors: [UIColor] = [.green, .yellow, .blue, .purple, .gray, .red]
override func viewDidLoad() {
super.viewDidLoad()
registerCollectionViewCustomCell()
prepareUI()
setupViews()
setCollectionViewLayoutToHorizontal()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//collectionView is instantiated as a computed property. Initialized with a system flow layout. The frame is initially assigned to CGRect.zero because it is controlled by the constrains
lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
let cv = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
cv.backgroundColor = .white
cv.layer.cornerRadius = 8
cv.isPagingEnabled = true
cv.delegate = self
cv.dataSource = self
return cv
}()
}
扩展1
extension ViewController {
private func prepareUI() {
view.backgroundColor = UIColor.black
navigationController?.navigationBar.barTintColor = .white
navigationItem.title = "Collection View"
}
private func registerCollectionViewCustomCell() {
collectionView.register(CustomCollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}
private func setCollectionViewLayoutToHorizontal() {
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
}
}
private func setupViews() {
view.addSubview(collectionView)
view.addConstraintsWithFormat(format: "H:|-15-[v0]-15-|", views: collectionView)
view.addConstraintsWithFormat(format: "V:|-80-[v0]-140-|", views: collectionView)
}
}
分机2:
//configuring the dataSource and the delegate methods for the collectionView
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CustomCollectionViewCell
cell.backgroundColor = colors[indexPath.item]
cell.wordLabel.text = sampleWords[indexPath.item]
print("_____________________________________")
print(cell.isHidden)
if cell.isHidden {
print("CUSTOM CELL INSTANCE NOT CREATED")
}
print("_____________________________________")
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sampleWords.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let size = CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
return size
}
}
CustomCollectionViewCell
类:
//custom cell class
class CustomCollectionViewCell: UICollectionViewCell {
override init(frame: CGRect) {
super.init(frame: frame)
setupViews()
print("instance of CustomCollectionViewCell is created")
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let wordLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private func setupViews() {
addSubview(wordLabel)
wordLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
addConstraint(NSLayoutConstraint(item: wordLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 70))
}
}
辅助方法addConstraintsWithFormat:
//helper method to add constraints to a view
extension UIView {
func addConstraintsWithFormat(format: String, views: UIView...){
var viewsDictionary = [String : UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
view.translatesAutoresizingMaskIntoConstraints = false
viewsDictionary[key] = view
}
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: format, options: NSLayoutFormatOptions(), metrics: nil, views: viewsDictionary))
}
}
这就是我所有的代码。 无需编程即可完成没有storyboard
一切。
为了调试并了解发生了什么,我添加了一些print语句:1)“ Print”语句,如果隐藏了单元格,并且如果cell.isHidden-> true->的实例,则输出布尔值自定义CollectionViewCell
尚未创建。 2)init方法中自定义CollectionViewCell
类中的“ Print”语句,以查看是否已创建单元格。
输出始终为以下内容:
instance of CustomCollectionViewCell is created
______________________________________
false
______________________________________
instance of CustomCollectionViewCell is created
______________________________________
false
______________________________________
instance of CustomCollectionViewCell is created
______________________________________
false
______________________________________
______________________________________
true
CUSTOM CELL INSTANCE NOT CREATED
之后,cell.Hidden始终返回true值。
我发现有些人正在努力解决相同的问题。 但是解决方案对我没有帮助。 因为我不在任何地方使用方法collectionView.reloadData()
,所以无法更改单个单元格的大小。 UICollectionViewCell被随机隐藏
UICollectionView使屏幕上立即可见的单元格isHidden = false
脱离队列,而正在准备的其他单元格isHidden = true
离开屏幕。 这反映了显示视图时这些单元格的实际状态。 您不能通过设置hidden = false
来覆盖此行为或isHidden
的状态。 单元格滚动进出视图时,UICollectionView会自动更新此状态。
我实现了类似的结构,其中UICollectionViewCell包含另一个UICollectionView。 (在该单元格中水平滚动,第二个垂直滚动。)我可以明确地指出,对于UICollectionView单元,不必正确地将isHidden = false
为正确放置该单元的子视图。 听起来您好像在想这个隐藏的属性是您遇到问题的原因(我一直走在那条精确的思路之列),但实际上您错了,而问题的原因还有其他原因。
就我而言,记录下来,问题单元报告了自动布局约束冲突,由于我认为这无关紧要,所以我一直忽略了这一冲突。 解决方法是,在将子视图添加到UICollectionViewCell之后,需要在单元格视图上调用layoutSubviews()
,并在其中包含的集合视图上调用reloadData()
。 如果不这样做,则嵌入式集合有时会起作用,但是当它重新使用以前创建的单元而不是创建新单元时,由于自动布局冲突,它们将无法显示。 这意味着要到达那里有特定的路径可以显示或不显示问题,具体取决于在先前视图中创建的可重复使用的单元格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.