UICollectionViewCell 标签问题

UICollectionViewCell issue with label

我是 Swift 和 iOS 的新手。我正在以编程方式创建一个 UICollectionView,并且一切正常,但是由于单元格的出列,当我滚动 collection-View 时,单元格中的标签重叠。我该如何解决?

这是从左到右滚动几次后的图片

这是我的代码:

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])");
    }

和 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;
    }

考虑到在调用 cellForItemAt 时总是将新标签添加到(重复使用的)单元格。

这是 Swift:没有尾随分号

保持您的设计的一个可能的解决方案是向标签添加标签并检查标签是否已经存在。

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
}