CollectionView中的按钮未出现

问题描述 投票:0回答:3

我想在CollectionView单元格中创建一个按钮。但是它没有出现。我的约束正确吗?

这是我的代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    if let vc = cell.viewWithTag(111) {
       vc.backgroundColor = Colors[indexPath.row]
    }

    let nextButton = UIButton()
    nextButton.translatesAutoresizingMaskIntoConstraints = false
    nextButton.setImage(UIImage(systemName: "gear"), for: .normal)
    nextButton.tintColor = UIColor.black
    nextButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
    nextButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
    nextButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
    view.addSubview(nextButton)
    nextButton.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    nextButton.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 100).isActive = true


    return cell
}

@IBAction func editButtonTapped() -> Void {
    print("Hello Edit Button")
}
ios swift button collectionview
3个回答
0
投票

您为什么要这样做?只需通过情节提要添加它? CellForRowAt将被多次调用,并反复添加按钮和约束(可能是为什么它不起作用)。如果您以编程方式进行所有操作,则应考虑在init方法中添加约束。

我找不到约束有什么问题。只需尝试更改添加它们的位置即可。

另外,为了调试视图布局,请考虑在Xcode中使用Debug View Hierarchy来查看按钮是否被正确添加。


0
投票

代替视图将其添加到单元格contentView ...中

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        if let vc = cell.viewWithTag(111) {
           vc.backgroundColor = Colors[indexPath.row]
        }

        let nextButton = UIButton()
        nextButton.translatesAutoresizingMaskIntoConstraints = false
        nextButton.setImage(UIImage(systemName: "gear"), for: .normal)
        nextButton.tintColor = UIColor.black
        nextButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
        nextButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
        nextButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
        cell.contentView.addSubview(nextButton)
        nextButton.centerXAnchor.constraint(equalTo: cell.contentView.centerXAnchor).isActive = true
        nextButton.bottomAnchor.constraint(equalTo: cell.contentView.bottomAnchor, constant: 100).isActive = true


        return cell
    }

但是在cellForRow中添加按钮不是一个好方法……您应该有一个自定义单元格类。在自定义类init方法中添加此按钮


0
投票

您似乎正在将按钮添加到UIViewControllerview而不是cell。我已经进行了更正,并使您的代码在Xcode-playground上运行。我将其张贴在这里,并在您的操场上进行尝试,并亲自检查其工作原理。

import UIKit
import PlaygroundSupport

class ViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        30
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = .red

        let nextButton = UIButton()
        nextButton.translatesAutoresizingMaskIntoConstraints = false
        nextButton.setImage(UIImage(systemName: "gear"), for: .normal)
        nextButton.tintColor = UIColor.black
        nextButton.addTarget(self, action: #selector(editButtonTapped), for: .touchUpInside)
        nextButton.heightAnchor.constraint(equalToConstant: 100).isActive = true
        nextButton.widthAnchor.constraint(equalToConstant: 100).isActive = true
        cell.addSubview(nextButton)
        nextButton.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
        nextButton.bottomAnchor.constraint(equalTo: cell.bottomAnchor).isActive = true

        return cell
    }

    @IBAction func editButtonTapped() -> Void {
        print("Hello Edit Button")
    }

}

let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: 150, height: 150)
PlaygroundPage.current.setLiveView(ViewController(collectionViewLayout: layout))
PlaygroundPage.current.needsIndefiniteExecution = true
© www.soinside.com 2019 - 2024. All rights reserved.