![](/img/trans.png)
[英]UITableView didSelectRowAt is not called iOS 10, but works for 9.3.5
[英]Swift didSelectRowAt on UITableView and UICollectionView not works
我在ViewController
创建tableView
和collectionView
,并将Delegate和DataSource设置为具有扩展名或包含该类的ViewController
。
该单元格已经正确显示在两个单元格上,我的UITapGestureRecognizer
中没有任何UITapGestureRecognizer
,但是当我想点击我的单元格时, didSelectRowAt
没有被调用。
我添加了canEditRowAt
,它可以工作,但didSelectRowAt
不能使用
我的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
}
}
谢谢。
如果您的表格视图处于编辑模式,例如
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.