[英]DidselectRow delegate method not working in swift
我有一个警报,其中我使用表视图显示了一些数据。
用户可以从表视图数据中选择任何项目。
现在,当我尝试在didSelectRow
方法中选择任何项目时,就没有选择任何项目。 我也使用过断点,但是它也没有捕获断点。
我已经给它的委托人和数据源一切都很好,但是我很困惑为什么它不起作用?
我的代码是这样的:
extension YourOrdersViewController : UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return PreOrderService.instance.PreOrderModelInstance.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Hello")
print(PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time)
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
if tableView == orderTableView{
print("Dishes")
}
else{
orderTableView.cellForRow(at: indexPath)?.accessoryType = .none
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = orderTableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath) as!
PreOrderTableViewCell
cell.titleLbl.text = PreOrderService.instance.PreOrderModelInstance[indexPath.row].perorder_time
print(cell.titleLbl.text!)
cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none
cell.accessoryType = cell.isSelected ? .checkmark : .none
return cell
}
}
View Controller如下所示, 在此处输入图像描述
它在UIViewControlller上是否有任何手势识别器。 如果是这样,并且手势识别器位于响应者链上的表格视图之上,则只需触摸一下,就不会留下任何表格视图。
我认为您代码中的整个问题是:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
self.tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
确保已设置委托和数据源。 如果要通过情节提要板创建视图,请确保为委托/数据源设置iboutlet。
这两行含义相同:因此删除第二行。
cell.selectionStyle = .none
cell.selectionStyle = UITableViewCellSelectionStyle.none
在Main.storyboard中选择您的表视图,然后在“属性检查器”中将选择更改为单个选择
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.