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

bkw*_*ero 13 uitableview swift

这是我的应用程序的流程:

首先,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)
Run Code Online (Sandbox Code Playgroud)

此时,将填充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")

}
Run Code Online (Sandbox Code Playgroud)

而且,我已正确设置dataSource和委托.

我还想澄清一下tableView是否正确填充和滚动; 当我点击一个单元格时它就不会做任何事情.

更新:

我发现由于某种原因,我可以在按住它们时选择细胞.这不是我想要的,所以有人知道如何解决这个问题吗?

Scr*_*ble 19

我刚刚使用你的代码来创建一个简单的表,选择工作正常并按预期注销HELLO.您可以在属性检查器中检查选择的值吗?这是我的,其选择设置为单选.

在此输入图像描述

这是我用于简单表的代码

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")
    }
}
Run Code Online (Sandbox Code Playgroud)

我也尝试隐藏并显示tableView,这对选择没有影响.

编辑和解决方案:

在下面的评论中,我们发现该问题与视图上的tapGestureRecogniser有关,这由op识别,只能通过按住单元格进行选择.在做出选择之前,手势必须失败,操作设法通过参考其他SO答案来解决问题

  • 你在视图上有tapGestureRecogniser吗? (4认同)
  • 是的,我确实......我从未认为那是问题所在.谢谢,我会为此找一个解决方法. (3认同)

小智 6

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

先检查手势.

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


mra*_*214 5

我的问题是由视图控制器本身上的点击手势识别器引起的(我有 BaseTableViewController 我从中扩展)。可能它干扰了 UITableView 的手势识别器。