2

我不知道从哪里开始,我有一个 UICollectionView,其中有多个填充了 UIImage 的单元格。我希望每个单元格/UIImage 在指尖上执行不同的操作。有人可以在这里指出我正确的方向吗?

我拥有的动作是来自 UIButton 的 @IBAction,我知道想要在 UICollectionView 中的单元格上执行该动作。

(我想我必须对 'let cell = countryCollectionView.dequeueReusableCell' 做点什么?

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    @IBOutlet weak var soundsCollectionView: UICollectionView!
    
    lazy var cv: UICollectionView = {
    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical
        
    var cc = UICollectionView(frame: .zero, collectionViewLayout: layout)
    cc.translatesAutoresizingMaskIntoConstraints = false
    cc.register(CustomCell.self, forCellWithReuseIdentifier: "CustomCell")
    cc.delegate = self
    cc.dataSource = self
    cc.backgroundColor = .white
    return cc
    }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view.
            
            view.addSubview(cv)
            cv.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
            cv.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
            cv.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
            cv.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
            
        }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.tag = indexPath.row
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 100)
    }
    
}

class CustomCell: UICollectionViewCell {
    
    lazy var centerImageView: UIImageView = {
        var img = UIImageView()
        img.translatesAutoresizingMaskIntoConstraints = false
        img.image = UIImage(named: "1")
        img.clipsToBounds = true
        img.isUserInteractionEnabled = true
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handler(_:)))
        tapGesture.numberOfTapsRequired = 1
        
        img.addGestureRecognizer(tapGesture)
        
        return img
    }()
    
    @objc private func handler(_ sender: UITapGestureRecognizer) {
        print("tapped tag > ", self.tag)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(centerImageView)
        centerImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        centerImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        centerImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        centerImageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
4

2 回答 2

1

我为您的问题解决方案编辑为新的编程示例。

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
    lazy var cv: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        
        var cc = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cc.translatesAutoresizingMaskIntoConstraints = false
        cc.register(CustomCell.self, forCellWithReuseIdentifier: "CustomCell")
        cc.delegate = self
        cc.dataSource = self
        cc.backgroundColor = .white
        return cc
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(cv)
        cv.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        cv.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        cv.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        cv.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
    
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.tag = indexPath.row
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 100)
    }
    
    
}


class CustomCell: UICollectionViewCell {
    
    lazy var centerImageView: UIImageView = {
        var img = UIImageView()
        img.translatesAutoresizingMaskIntoConstraints = false
        img.image = UIImage(named: "1")
        img.clipsToBounds = true
        img.isUserInteractionEnabled = true
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handler(_:)))
        tapGesture.numberOfTapsRequired = 1
        
        img.addGestureRecognizer(tapGesture)
        
        return img
    }()
    
    @objc private func handler(_ sender: UITapGestureRecognizer) {
        print("tapped tag > ", self.tag)
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        addSubview(centerImageView)
        centerImageView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
        centerImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
        centerImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
        centerImageView.widthAnchor.constraint(equalToConstant: 80).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
于 2020-07-11T22:11:47.033 回答
0

在这种情况下,我会避免在您的单元格上使用按钮。相反,您应该挂钩didSelectItemAt 委托方法,并将必要的信息添加到数据模型中:

struct Country {
    let imageName: String
    let sound: Sound // You didn't specify what type the sound1 object is but you get the gist
}

因此,您的国家/地区数组现在将包含这个新结构,而不仅仅是原始字符串:

let countries = [
    Country("country1", sound1),
    Country("country2", sound2),
    ...
]

然后,您可以从传递给 didSelectItemAt 的 indexPath 中获得您想要播放的确切声音:

let sound = self.countries[indexPath.row].sound
sound.play()

您还需要调整在 cellForItemAt 中设置单元格图像的方式:

let imageName = self.countries[indexPath.row].imageName
cell.countryImageView.image = UIImage(named: imageName)
于 2020-07-15T14:05:52.733 回答