繁体   English   中英

UITableView和UICollectionView上的Swift didSelectRowAt不起作用

[英]Swift didSelectRowAt on UITableView and UICollectionView not works

我在ViewController创建tableViewcollectionView ,并将Delegate和DataSource设置为具有扩展名或包含该类的ViewController

该单元格已经正确显示在两个单元格上,我的UITapGestureRecognizer中没有任何UITapGestureRecognizer ,但是当我想点击我的单元格时, didSelectRowAt没有被调用。

我添加了canEditRowAt ,它可以工作,但didSelectRowAt不能使用

这是我的CollectionView

这是我的TableView具有编辑操作,它可以正常工作

这是我的ViewController场景

我的TableView已设置:

- UserInteractionEnable = true 
- Selection = Single Selection

我的Xib文件:

- UserInteractionEnable = true 

这是我的课:

class ProfileVC: UIViewController {

    @IBOutlet weak var profileTableView: UITableView!

    var profileTitle = DataService.instance.profileTitle

    override func viewDidLoad() {
        super.viewDidLoad()
        configureTableView()
    }

    func configureTableView() {
        let nib = UINib(nibName: "ProfileCell", bundle: nil)
        profileTableView.register(nib, forCellReuseIdentifier: "ProfileCell")
        profileTableView.delegate = self
        profileTableView.dataSource = self
        profileTableView.allowsSelection = true
    }

}

extension ProfileVC: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return profileTitle.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileCell", for: indexPath) as? ProfileCell else { return ProfileCell() }
        cell.configureCell(withTitle: profileTitle[indexPath.row])
        return cell
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Selected")
    }

}

这是我的ProfileCellClass:

class ProfileCell: UITableViewCell {

    @IBOutlet weak var titleLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }

    func configureCell(withTitle title: String){
        self.titleLabel.text = title
    }   
}

这是我的.xib文件的详细信息

谢谢。

如果您的表格视图处于编辑模式,例如

tableView.setEditing(true, animated: false)

你需要设置

tableView.allowsSelectionDuringEditing = true

在您的ConfigureTableView方法中,也注册您的TableViewCell类。

profileTableView.register(ProfileCell.self, forCellReuseIdentifier: "ActivityImageCell")
profileTableView.register(UINib(nibName: "ProfileCell", bundle: nil), forCellReuseIdentifier: "ProfileCell")

这可能是一个原因

暂无
暂无

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

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