首页 > 解决方案 > 无法选择 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
        }
    }
}

标签: iosswiftxcodeuitableview

解决方案


我发现发生了什么。所有视图控制器都从“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 方法中。


推荐阅读