【问题标题】:Swift didSelectRowAt on UITableView and UICollectionView not worksUITableView 和 UICollectionView 上的 Swift didSelectRowAt 不起作用
【发布时间】:2018-05-09 10:19:13
【问题描述】:

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

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

我'已添加 canEditRowAt 并且它可以工作,但不适用于 didSelectRowAt

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

谢谢。

【问题讨论】:

  • 你在哪里添加profileTableView?你能滚动它吗?
  • 在 viewDidLoad() 中,当我滚动 UITableView 或 UICollectionView 时,它会弹跳
  • 你能否尝试实现另一个 UITableView 委托方法(如estimatedHeightForRowAt)并确保它被调用?
  • 您没有 IBOutlet 甚至没有 profileTableView 的实例变量?

标签: ios swift uitableview uicollectionviewcell uitableviewrowaction


【解决方案1】:

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

tableView.setEditing(true, animated: false)

你需要设置

tableView.allowsSelectionDuringEditing = true

【讨论】:

    【解决方案2】:

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

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

    这可能是一个原因

    【讨论】:

    • 您是否可以进行任何类型的交互,例如您可以向上或向下滚动 tableView。并确保您的渐变视图位于 tableView 后面。
    • 你试过这个profileTableView.allowsSelectionDuringEditing = true
    • 可以,我的 canEditRowAt 工作正常,编辑和删除工作正常,但 didSelectItemAt 不行;(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2018-04-20
    • 1970-01-01
    • 2019-06-25
    • 2018-11-03
    相关资源
    最近更新 更多