为什么我无法选择TableView的单元格?

14

这是我的应用程序流程:

首先,TableView被设置为隐藏状态。屏幕中心有一个UITextField。当用户输入内容并点击“Go”按钮时,会运行以下代码:

self.view.layoutIfNeeded()
UIView.animateWithDuration(0.5, animations: {

    self.textFieldConstraint.constant = -230
    self.tableView.hidden = false
    self.goButton.hidden = true
    self.view.layoutIfNeeded()

}, completion: nil)

在这一点上,tableview已经被填充。当选择一行时,我需要处理正在填充它的数据。

然而,当我点击单元格时,完全没有任何反应。

我做错了什么?

这是我的TableView代码:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell: SearchResultsTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! SearchResultsTableViewCell

    cell.label.text = searchResultsNames[indexPath.row]

    return cell
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return searchResultsUrls.count

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    print("HELLO")

}

同时,我已经正确设置了数据源和代理。

我还想澄清一下,tableView可以正确地填充和滚动;只是当我点击单元格时它不会执行任何操作。

更新:

我发现出现了一个问题,即当我按住单元格时,可以选择单元格。这不是我想要的,有人知道如何解决吗?

8个回答

27

我刚刚使用了你的代码创建了一个简单表格,选择功能正常,并按预期记录下“HELLO”。你能检查一下属性检查器中Selection的值吗?这是我的设置,其中Selection设置为Single Selection。

输入图片说明

这是我用于创建简单表格的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var tableView: UITableView!

    var searchResults = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        searchResults.append("Testing 1")
        searchResults.append("Testing 2")
        searchResults.append("Testing 3")
        searchResults.append("Testing 4")
        searchResults.append("Testing 5")

        tableView.dataSource = self
        tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("tableCell", forIndexPath: indexPath)
        cell.textLabel?.text = searchResults[indexPath.row]

        return cell
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searchResults.count
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        print("HELLO")
    }
}

我还尝试了隐藏和显示tableView,但对选择没有影响。

编辑和解决方案:

在下面的评论中,我们发现问题与视图上的tapGestureRecogniser相关。这是通过只能通过在单元格上按住轻敲来进行选择而被op识别出来的。在选择可以进行之前,手势必须失败,op通过参考这个SO Answer来解决问题。


5
您的视图上是否有一个"tapGestureRecogniser"手势识别器? - Scriptable
3
是的,我是这样想的……我从未考虑过这是个问题。谢谢,我会尝试解决这个问题。 - swiftyboi
1
请查看此其他答案,https://dev59.com/3nVC5IYBdhLWcg3wjx1d - Scriptable

9

如果您使用“点击”手势,则无法选择表格单元格。 (但是,如果您点击并向右拖动单元格,则可以选择它。)

首先检查手势。

如果您的代码中有self.tableView.allowsSelection = false,请将false替换为true或删除此行。


5

我的问题是由于视图控制器本身的轻拍手势识别器引起的(我正在扩展BaseTableViewController)。 很可能它正在干扰UITableView的手势识别器。


非常感谢,您的回答正好解决了我的问题。 - Sakthimuthiah

4
如果你有一个手势识别器,只需输入 gestureRecognizer.cancelsTouchesInView = false

谢谢,我的主视图中有一个轻拍手势识别器,表视图在全屏模态下。这节省了我的时间 :) - nmy

4
在您的viewDidLoad或者您设置视图的任何地方,确保您的表视图允许选择。这可以通过allowSelection属性进行控制。
例如:
override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.allowsSelection = true
}

2

针对我的情况,我正在实现另一个did select row方法,所以我把它删除了,然后键入“didSelect...”并在建议的方法中选择第一个方法,Swift 3版本如下:

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print("Row #: \(indexPath)")
}

0

尝试在你的tableView的属性检查器中禁用并重新启用用户交互 Enabled 属性 看起来像这样


-2

我遇到了和你一样的问题,通过删除下面的代码解决了它。

self.view.layoutIfNeeded()

你可以试试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接