ios - 无法选择 tableview 的单元格
问题描述
我正在使用 Xcode 11.2,并且我有一个包含 3 个表视图的视图。它们都有代码的出口,它们的委托和数据源在 viewDidLoad 方法中正确设置。
发生的情况是,单击时单元格会突出显示,但随后它不会保持选中状态(didSelectRow 永远不会被调用)。
如果我用鼠标左键和右键快速单击多次,最终将选择单元格并调用 didSelectRow。
我还注意到,如果我单击单元格并同时稍微滚动表格,选择几乎总是有效的。
这里有一些代码:
@IBOutlet weak var linesTableView: UITableView!
@IBOutlet weak var firstLvlTableView: UITableView!
@IBOutlet weak var secondLvlTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.linesTableView.dataSource = self
self.linesTableView.delegate = self
self.firstLvlTableView.delegate = self
self.firstLvlTableView.dataSource = self
self.secondLvlTableView.dataSource = self
self.secondLvlTableView.delegate = self
if (CLLocationManager.locationServicesEnabled()) {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
self.secondLvlTableView.isHidden = true
self.firstLvlTableView.isHidden = false
self.linesTableView.isHidden = true
fetchData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == linesTableView {
return 0
} else if tableView == firstLvlTableView {
return self.salesAssistantLocalized.count //viewModel.typesOfSale.lists.endIndex
} else if tableView == secondLvlTableView {
return 4 //viewModel.standardTableLevels.lists.endIndex
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == firstLvlTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "FirstLvlCell", for: indexPath)
cell.textLabel?.text = self.salesAssistantLocalized[indexPath.row]
return cell
} else if tableView == secondLvlTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "SecondLvlCell", for: indexPath)
cell.textLabel?.text = self.levels[indexPath.row]
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SaleTypeCell", for: indexPath)
return cell
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
printDebug("Did Select Row \(indexPath.row) for table \(tableView)")
if tableView == firstLvlTableView {
if indexPath.row == 0 {
self.secondLvlTableView.isHidden = false
}
}
}
解决方案
我发现发生了什么。所有视图控制器都从“BaseViewController”扩展而来,该“BaseViewController”具有用于关闭键盘的轻击手势识别器。这就是问题所在。
我只是添加了一个 Bool 来选择是否使用它:
open var useTapGesture: Bool = true
然后用这个包装点击手势代码:
if useTapGesture {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(BaseViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
}
这样我可以使用
useTapGesture = false
在我的 viewDidLoad 方法中。
推荐阅读
- html - 当我使用百分比(CSS HTML)时,div的高度没有增长
- hive - 如何使 Hive 表始终可用于下游
- python - 熊猫:一列是另一列的子字符串
- arrays - 根据字符串变量获取Bash数组键值
- react-native - react-native Expo CLI 给出 EPERM 错误:\table.xsl.tmp 不允许操作
- css - 如何解决仅在 Safari Mobile 中出现的 CSS 问题?
- python - 如何在读取文件但删除一个变量然后替换它时删除“\n”字符
- flutter - 无法设置回调函数。错误说:“DropDown”必须有一个方法体,因为“ListLayout”不是抽象的
- android - 如何在apache cordova中完成页面滑动
- django - serializer.save() 不将数据保存到管理数据库