为什么我不能选择TableView单元格呢?

时间:2022-01-13 20:39:36

This is the flow of my app:

这是我的应用流程:

First, the TableView is set to hidden. There is a UITextField in the center of the screen. When the user types something and hits Go, this code is run:

首先,将TableView设置为hidden。在屏幕中心有一个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)

At this point, the tableview is populated. When a row is selected, I need to manipulate the data that is populating it.

此时,将填充tableview。当选定一行时,我需要操作填充该行的数据。

However, absolutely nothing happens when I tap a cell.

然而,当我点击一个单元格时,绝对不会发生任何事情。

What am I doing wrong?

我做错了什么?

My TableView code is here:

我的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")

}

And, I have set the dataSource and delegate properly.

我已经正确地设置了数据源和委托。

I also want to clarify that the tableView populates and scrolls properly; it just won't do anything when I tap a cell.

我还想澄清一下tableView是否正确填充和滚动;当我点击一个单元时,它什么都不会做。

Update:

更新:

I've discovered that for some reason, I can select the cells when I press and hold them. It is not what I want, so does anybody know how to fix this?

我发现出于某种原因,我可以在按下并按住单元格时进行选择。这不是我想要的,有人知道怎么解决吗?

6 个解决方案

#1


15  

I have just used your code to create a simple table, selection is working fine and logging out HELLO as expected. Can you check the values of Selection in the attributes inspector? Here is mine, which has Selection set to Single Selection.

我刚刚使用您的代码创建了一个简单的表,选择功能良好,并按预期的那样登出HELLO。你能在属性检查器中检查选择的值吗?这是我的,它的选择集是单选择。

为什么我不能选择TableView单元格呢?

And here is the code I used for my simple table

这是我用于简单表的代码

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")
    }
}

I also tried hiding and showing the tableView which made no difference on selection.

我还试图隐藏和显示tableView,这对选择没有影响。

EDIT & SOLUTION:

编辑和解决方案:

In the comments below, we discovered that the issue is related to a tapGestureRecogniser on the view, This was identified by the op only being able to make a selection by holding a tap on the cell. The gesture has to fail before the selection can be made, the op managed to solve the problem by referring to this other SO Answer

在下面的评论中,我们发现这个问题与视图上的tapGestureRecogniser有关,这个问题被op识别出来,它只能通过在单元格上单击来进行选择。在做出选择之前,这个手势必须失败,op通过引用另一个SO来解决这个问题

#2


3  

if you use "tap" gesture, you can't select table cell. (but, if you click and drag to right a cell, you can select it.)

如果使用“tap”手势,则不能选择表格单元格。(但是,如果您单击并拖动到单元格的右边,您可以选择它。)

Check gesture first.

检查动作。

And if your code has self.tableView.allowsSelection = false, replace false to true or delete this line.

如果你的代码有self。tableview。allowsSelection = false,将false替换为true或删除该行。

#3


2  

My problem has been caused by the tap gesture recognizer on the view controller itself (I had BaseTableViewController I was extending from). Probably it was interfering with the gesture recognizers of UITableView.

我的问题是由视图控制器本身的tap手势识别器引起的(我有一个BaseTableViewController我正在扩展)。它可能干扰了UITableView的手势识别器。

#4


1  

In your viewDidLoad, or wherever you set up your view, make certain that your table view even allows selections. That can be controlled with the allowSelection property.

在viewDidLoad中,或者设置视图的任何地方,确保您的表视图甚至允许选择。它可以用allowSelection属性进行控制。

Something like:

喜欢的东西:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.allowsSelection = true
}

#5


1  

For me I was implementing another did select row method, so I erased it and typed "didSelect..." and selected the first one in the suggested methods, which is this for swift 3:

对我来说,我正在实现另一个didselecrowmethod,所以我删除了它,并输入“didSelect…”,并在建议的方法中选择了第一个,这是swift 3:

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

#6


0  

I met the same problem as you and solved it by removing the below code

我遇到了与您相同的问题,通过删除下面的代码来解决它

self.view.layoutIfNeeded()

you can try it.

你可以试一试。

#1


15  

I have just used your code to create a simple table, selection is working fine and logging out HELLO as expected. Can you check the values of Selection in the attributes inspector? Here is mine, which has Selection set to Single Selection.

我刚刚使用您的代码创建了一个简单的表,选择功能良好,并按预期的那样登出HELLO。你能在属性检查器中检查选择的值吗?这是我的,它的选择集是单选择。

为什么我不能选择TableView单元格呢?

And here is the code I used for my simple table

这是我用于简单表的代码

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")
    }
}

I also tried hiding and showing the tableView which made no difference on selection.

我还试图隐藏和显示tableView,这对选择没有影响。

EDIT & SOLUTION:

编辑和解决方案:

In the comments below, we discovered that the issue is related to a tapGestureRecogniser on the view, This was identified by the op only being able to make a selection by holding a tap on the cell. The gesture has to fail before the selection can be made, the op managed to solve the problem by referring to this other SO Answer

在下面的评论中,我们发现这个问题与视图上的tapGestureRecogniser有关,这个问题被op识别出来,它只能通过在单元格上单击来进行选择。在做出选择之前,这个手势必须失败,op通过引用另一个SO来解决这个问题

#2


3  

if you use "tap" gesture, you can't select table cell. (but, if you click and drag to right a cell, you can select it.)

如果使用“tap”手势,则不能选择表格单元格。(但是,如果您单击并拖动到单元格的右边,您可以选择它。)

Check gesture first.

检查动作。

And if your code has self.tableView.allowsSelection = false, replace false to true or delete this line.

如果你的代码有self。tableview。allowsSelection = false,将false替换为true或删除该行。

#3


2  

My problem has been caused by the tap gesture recognizer on the view controller itself (I had BaseTableViewController I was extending from). Probably it was interfering with the gesture recognizers of UITableView.

我的问题是由视图控制器本身的tap手势识别器引起的(我有一个BaseTableViewController我正在扩展)。它可能干扰了UITableView的手势识别器。

#4


1  

In your viewDidLoad, or wherever you set up your view, make certain that your table view even allows selections. That can be controlled with the allowSelection property.

在viewDidLoad中,或者设置视图的任何地方,确保您的表视图甚至允许选择。它可以用allowSelection属性进行控制。

Something like:

喜欢的东西:

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.allowsSelection = true
}

#5


1  

For me I was implementing another did select row method, so I erased it and typed "didSelect..." and selected the first one in the suggested methods, which is this for swift 3:

对我来说,我正在实现另一个didselecrowmethod,所以我删除了它,并输入“didSelect…”,并在建议的方法中选择了第一个,这是swift 3:

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

#6


0  

I met the same problem as you and solved it by removing the below code

我遇到了与您相同的问题,通过删除下面的代码来解决它

self.view.layoutIfNeeded()

you can try it.

你可以试一试。