简体   繁体   English

UITableView和UICollectionView上的Swift didSelectRowAt不起作用

[英]Swift didSelectRowAt on UITableView and UICollectionView not works

I make tableView and collectionView in my ViewController and was set the Delegate and DataSource to my ViewController with extension or include the class. 我在ViewController创建tableViewcollectionView ,并将Delegate和DataSource设置为具有扩展名或包含该类的ViewController

The cell already show correctly on both of them, I haven't any UITapGestureRecognizer in my Class but when I want to tap on my Cell the didSelectRowAt not called. 该单元格已经正确显示在两个单元格上,我的UITapGestureRecognizer中没有任何UITapGestureRecognizer ,但是当我想点击我的单元格时, didSelectRowAt没有被调用。

I've added canEditRowAt and It's work but not with didSelectRowAt 我添加了canEditRowAt ,它可以工作,但didSelectRowAt不能使用

这是我的CollectionView

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

This is my ViewController Scene 这是我的ViewController场景

My TableView already set : 我的TableView已设置:

- UserInteractionEnable = true 
- Selection = Single Selection

My Xib Files : 我的Xib文件:

- UserInteractionEnable = true 

This is my class : 这是我的课:

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

}

This is my ProfileCellClass : 这是我的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
    }   
}

This my .xib file details 这是我的.xib文件的详细信息

Thank you. 谢谢。

If your table view is in editing mode like 如果您的表格视图处于编辑模式,例如

tableView.setEditing(true, animated: false)

you need to set 你需要设置

tableView.allowsSelectionDuringEditing = true

In your ConfigureTableView method register your TableViewCell Class as well. 在您的ConfigureTableView方法中,也注册您的TableViewCell类。

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

This could be a reason 这可能是一个原因

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

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