简体   繁体   English

label 的 UICollectionViewCell 问题

[英]UICollectionViewCell issue with label

I am new to Swift and iOS.我是 Swift 和 iOS 的新手。 I am creating a UICollectionView programmatically and every thing is working fine but due to the dequeuing of the cell the labels in the cells are overlapping when I scroll the collection-View.我正在以编程方式创建 UICollectionView 并且一切正常,但是由于单元格的出列,当我滚动集合视图时,单元格中的标签重叠。 How should I solve this?我应该如何解决这个问题?

Here is the pic after scrolling some times from left to right这是从左到右滚动几次后的图片

在此处输入图像描述

Here is my code:这是我的代码:

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 12;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath);
        cell.backgroundColor = .red;
        
        let label = UILabel();
        label.textAlignment = .center
        label.text = "\(indexPath.row)";
        cell.contentView.addSubview(label);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true;
        label.rightAnchor.constraint(equalTo: cell.rightAnchor).isActive = true;
        label.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true;
        label.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true;
        
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("\(indexPath) wit text: \(data[indexPath.row])");
    }

and viewDidLoad:和 viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad();
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.scrollDirection = .horizontal;
        let collectionView = UICollectionView(frame: self.view.bounds, collectionViewLayout: flowLayout);
        collectionView.register(CollectionCell.self, forCellWithReuseIdentifier: "cell")
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = .green
                
        view.addSubview(collectionView);
        collectionView.translatesAutoresizingMaskIntoConstraints = false;
        collectionView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true;
        collectionView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true;
        collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -60).isActive = true;
        collectionView.heightAnchor.constraint(equalToConstant: 100).isActive = true;
    }

Consider that always a new label is added to the (reused) cell when cellForItemAt is called.考虑到当调用cellForItemAt时,总是将新的 label 添加到(重用)单元格中。

And this is Swift: No trailing semicolons .这是 Swift:没有尾随分号

A possible solution keeping your design is to add a tag to the label and to check if the label already exists.保留您的设计的一种可能的解决方案是向 label 添加标签并检查 label 是否已存在。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    cell.backgroundColor = .red
    let label : UILabel
    if let existingLabel = cell.contentView.viewWithTag(9999) as? UILabel {
        label = existingLabel
    } else {
        label = UILabel()
        label.tag = 9999
        label.textAlignment = .center
        cell.contentView.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        label.leftAnchor.constraint(equalTo: cell.leftAnchor).isActive = true
        label.rightAnchor.constraint(equalTo: cell.rightAnchor).isActive = true
        label.topAnchor.constraint(equalTo: cell.topAnchor).isActive = true
        label.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true
    }
    label.text = "\(indexPath.row)"
    return cell
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM