Swift didSelectRowAt在UITableView和UICollectionView上不起作用

时间:2018-05-09 10:19:14

标签: ios swift uitableview uicollectionviewcell uitableviewrowaction

我在tableView中设置了collectionViewViewController,并将Delegate和DataSource设置为带有扩展名的ViewController或包含该类。

单元格已经在两个单元格中都显示正确,我的班级中没有UITapGestureRecognizer,但是当我想点击我的单元格时didSelectRowAt没有被调用。< br />
我添加了canEditRowAt并且它已经工作但不是didSelectRowAt

This is my CollectionView

This is my TableView with edit action and it works

This is my ViewController Scene

我的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
    }   
}

This my .xib file details

谢谢。

2 个答案:

答案 0 :(得分:0)

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

tableView.setEditing(true, animated: false)

你需要设置

tableView.allowsSelectionDuringEditing = true

答案 1 :(得分:0)

在ConfigureTableView方法中也注册了TableViewCell类。

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

这可能是一个原因