长按 swift 后调用的 didselectrowatindexpath

didselectrowatindexpath called after long pressed in swift

我正在尝试 select select 单元格上的内容 来自 Table,这里我使用的是 didselectrowatindexpath 方法,但在长按单元格后它会被调用。

这可能是重复的问题,但我尝试了很多解决方案,但我的问题没有得到解决 这是我正在使用的代码

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

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    
    let cell  = tableView.dequeueReusableCellWithIdentifier("AutoCompleteRowIdentifier", forIndexPath: indexPath) as! DrawerTableViewCell
    let index = indexPath.row as Int
    
    cell.autoCompleteLabel!.text = autocompleteUrls[index].email!
    return cell
}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    print("You selected cell #\(indexPath.row)!")
    
    print("didSelectRowAtIndexPath")
    let selectedCell   = autocompleteTableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell
    print("Selected Table Text =\(selectedCell.autoCompleteLabel!.text)")
    textEmail.text = selectedCell.autoCompleteLabel!.text
    autocompleteTableView.hidden = true
    
}

我的viewDidLoad

   override func viewDidLoad() {
    pastUrls = defaults.objectForKey("autoCompleteEmail") as? [String] ?? [String]()
    spinnerInitialization()
    super.viewDidLoad()
    self.hideKeyboardWhenTappedAround()
    autocompleteTableView.tableFooterView = UIView()
    autocompleteTableView.hidden = true
    autocompleteTableView.delegate = self
    autocompleteTableView.dataSource = self
    textEmail.delegate = self
}

已更新:- hideKeyboardWhenTappedAround

的代码
    extension UIViewController {
func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
    view.addGestureRecognizer(tap)
}

func dismissKeyboard() {
    view.endEditing(true)
}}

为单元格内的标签启用 UserInteraction。

并验证单元格是否启用了 UserInteraction id。

你需要解释一下你的结构。我假设您在 UIViewController 中使用 tableView。 但是,为什么在 didSelect 方法中有以下行?

 let selectedCell   = autocompleteTableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell

而不是

let selectedCell   = tableView.cellForRowAtIndexPath(indexPath)! as! DrawerTableViewCell

我了解到您将 table 视图的名称指定为 autocompleteTableView,但是 table 视图的引用不是来自 didSelectRow 方法本身吗?

谢谢Dev.RK 在这里,我使用代码隐藏我在 hideKeyboardWhenTappedAround() 中使用 view.addGestureRecognizer(tap) 方法的键盘 @Dev.Rk建议我在view.addGestureRecognizer(tap)前加一行 该行是 tap.cancelsTouchesInView=false 并且解决了我的疯狂问题