Swift didSelectRowAt для UITableView и UICollectionView не работает - PullRequest
0 голосов
/ 09 мая 2018

Я делаю tableView и collectionView в моем ViewController, и для делегата и источника данных был установлен мой ViewController с расширением или включением класса.

Ячейка уже отображается правильно на обоих из них, у меня нет никаких UITapGestureRecognizer в моем классе, но когда я хочу нажать на свою ячейку, didSelectRowAt не вызывается.

I добавили canEditRowAt и это работает, но не с didSelectRowAt

This is my CollectionView

This is my TableView with edit action and it works

Это моя сцена 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

Спасибо.

Ответы [ 2 ]

0 голосов
/ 09 мая 2018

В вашем методе ConfigureTableView зарегистрируйте также ваш класс TableViewCell.

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

Это может быть причиной

0 голосов
/ 09 мая 2018

Если ваш табличный вид находится в режиме редактирования, например

tableView.setEditing(true, animated: false)

вам нужно установить

tableView.allowsSelectionDuringEditing = true
...