自定义UICollectionViewCell类中的按钮不起作用

侯赛因

嗨,我正在开发一个路线查找器应用程序,当用户搜索特定位置时,在应用程序内部,该应用程序在地图上和UICollectionView中都提供了位置数,其中包含找到的位置列表的简短描述以及一个“转到” UIButton以显示方向。但是,尽管按钮显示在单元格上并且是可触摸的,这意味着isUserInteractionEnabled为true,但按钮并未触发动作。控制台上未显示“已按下”。(注意:屏幕截图仅供演示。)

在MapViewController中声明UICollectionView。

let collectionViewOfListOfPlaces:UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.translatesAutoresizingMaskIntoConstraints = false
        cv.register(CustomCell.self, forCellWithReuseIdentifier: "cell")
        return cv
    }()

在MapViewController的viewDidLoad函数中,添加了这些行。

collectionViewOfListOfPlaces.reloadData()
collectionViewOfListOfPlaces.delegate = self

在MapViewController的扩展中,添加了以下几行功能。

func setupCollectionViewOfListOfPlaces(){
        hideListViewButton.translatesAutoresizingMaskIntoConstraints = false
        hideListViewButton.isUserInteractionEnabled = true
        collectionViewOfListOfPlaces.backgroundColor = UIColor.white.withAlphaComponent(0)
        collectionViewOfListOfPlaces.topAnchor.constraint(equalTo: listContentView.topAnchor, constant: 0).isActive = true
        collectionViewOfListOfPlaces.leadingAnchor.constraint(equalTo: listContentView.leadingAnchor, constant: 10).isActive = true
        collectionViewOfListOfPlaces.trailingAnchor.constraint(equalTo: listContentView.trailingAnchor, constant: -10).isActive = true
        collectionViewOfListOfPlaces.heightAnchor.constraint(equalToConstant: view.frame.height/5).isActive = true  // ?
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionViewOfListOfPlaces.frame.height/0.8, height: collectionViewOfListOfPlaces.frame.height/1.2)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return landmarks.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionViewOfListOfPlaces.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CustomCell
        let cellData = self.landmarks[indexPath.item]
        cell.backgroundColor = Colors.violet3
        cell.setData(dataa: cellData) //????????
        cell.delegate = self
        return cell
    }

自定义类:

import Foundation
import UIKit

protocol CustomCellDelegate {
    func didPrintNameOfPlace(placeName: String)
}

class CustomCell: UICollectionViewCell {
    
//    var data: Landmark? {
//        didSet {
//            guard let data = data else { return }
//
//        }
//    }
    let directButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Go", for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 18)
        button.addTarget(
            self,
            action: #selector(directButtonPressed),
            for: UIControl.Event.touchUpInside)
        button.backgroundColor = .white
        return button
    }()
    
    var data: Landmark?
    var delegate: CustomCellDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        setUpDirectButton()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func setData(dataa: Landmark){
        data = dataa
        
    }
    
    func setUpDirectButton(){
        contentView.addSubview(directButton)
        directButton.translatesAutoresizingMaskIntoConstraints = false
        directButton.isUserInteractionEnabled = true
        directButton.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true
        directButton.rightAnchor.constraint(equalTo: contentView.rightAnchor, constant:  0).isActive = true
        directButton.widthAnchor.constraint(equalToConstant: 50).isActive = true
        directButton.heightAnchor.constraint(equalToConstant: 50).isActive = true
        directButton.frame.size.width = 50
        directButton.frame.size.height = 50
    }
    
    @objc func directButtonPressed(sender: UIButton!) {
//        delegate?.didPrintNameOfPlace(placeName: data!.nameOfPlace)
        print("Pressed")
        
    }
}
```[Screen shot link][1]


  [1]: https://i.stack.imgur.com/azHSd.jpg
贾瓦德·阿里

用惰性标记您的按钮初始化

private lazy var directButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Go", for: .normal)
        button.titleLabel?.font = .systemFont(ofSize: 18)
        button.addTarget(
            self,
            action: #selector(directButtonPressed),
            for: UIControl.Event.touchUpInside)
        button.backgroundColor = .white
        return button
    }()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

按钮处理程序在自定义 MessageBox 类中不起作用

来自分类Dev

swiftui:自定义按钮在ListView中不起作用

来自分类Dev

自定义 UIView 类中的 UIButton 不起作用

来自分类Dev

UI小猫按钮自定义映射不起作用

来自分类Dev

设置后退按钮自定义外观不起作用

来自分类Dev

自定义按钮对IsMouseOver不起作用

来自分类Dev

Android自定义状态按钮不起作用

来自分类Dev

iOS 按钮在自定义视图中不起作用

来自分类Dev

添加自定义后退按钮对 UINavigationBar 不起作用

来自分类Dev

Vuetify 按钮自定义 css 颜色不起作用

来自分类Dev

Android 自定义通知按钮 onClick 不起作用

来自分类Dev

Symfony自定义类自动加载不起作用

来自分类Dev

Laravel自定义Facades类不起作用

来自分类Dev

自定义矩形类不起作用

来自分类Dev

CSS自定义单选按钮在IE 8中不起作用

来自分类Dev

自定义UITableViewCell,单元格中的按钮不起作用

来自分类Dev

链接中包装的自定义HTML按钮不起作用

来自分类Dev

无法添加自定义文本,单选按钮在Rails 4表单中不起作用

来自分类Dev

自定义UITableViewCell,单元格中的按钮不起作用

来自分类Dev

CSS自定义单选按钮在IE 8中不起作用

来自分类Dev

WordPress功能在自定义TinyMCE编辑器按钮中不起作用

来自分类Dev

Symfony 2中的自定义存储库类不起作用

来自分类Dev

spring-boot属性注入在自定义@Configuration类中不起作用

来自分类Dev

WP_Query()在自定义类中不起作用

来自分类Dev

自定义类中的信号和插槽(包括PyQt QWidget)不起作用

来自分类Dev

C#自定义类在WCF服务中不起作用

来自分类Dev

故事板中设置的自定义类的本地化不起作用

来自分类Dev

UITapGestureRecognizer在自定义类中不起作用(不是视图控制器)

来自分类Dev

NSDate 的自定义类排序属性在 Objective-C 中不起作用

Related 相关文章

  1. 1

    按钮处理程序在自定义 MessageBox 类中不起作用

  2. 2

    swiftui:自定义按钮在ListView中不起作用

  3. 3

    自定义 UIView 类中的 UIButton 不起作用

  4. 4

    UI小猫按钮自定义映射不起作用

  5. 5

    设置后退按钮自定义外观不起作用

  6. 6

    自定义按钮对IsMouseOver不起作用

  7. 7

    Android自定义状态按钮不起作用

  8. 8

    iOS 按钮在自定义视图中不起作用

  9. 9

    添加自定义后退按钮对 UINavigationBar 不起作用

  10. 10

    Vuetify 按钮自定义 css 颜色不起作用

  11. 11

    Android 自定义通知按钮 onClick 不起作用

  12. 12

    Symfony自定义类自动加载不起作用

  13. 13

    Laravel自定义Facades类不起作用

  14. 14

    自定义矩形类不起作用

  15. 15

    CSS自定义单选按钮在IE 8中不起作用

  16. 16

    自定义UITableViewCell,单元格中的按钮不起作用

  17. 17

    链接中包装的自定义HTML按钮不起作用

  18. 18

    无法添加自定义文本,单选按钮在Rails 4表单中不起作用

  19. 19

    自定义UITableViewCell,单元格中的按钮不起作用

  20. 20

    CSS自定义单选按钮在IE 8中不起作用

  21. 21

    WordPress功能在自定义TinyMCE编辑器按钮中不起作用

  22. 22

    Symfony 2中的自定义存储库类不起作用

  23. 23

    spring-boot属性注入在自定义@Configuration类中不起作用

  24. 24

    WP_Query()在自定义类中不起作用

  25. 25

    自定义类中的信号和插槽(包括PyQt QWidget)不起作用

  26. 26

    C#自定义类在WCF服务中不起作用

  27. 27

    故事板中设置的自定义类的本地化不起作用

  28. 28

    UITapGestureRecognizer在自定义类中不起作用(不是视图控制器)

  29. 29

    NSDate 的自定义类排序属性在 Objective-C 中不起作用

热门标签

归档