【问题标题】:didSelectRowAt indexPath does not when tapped (but does trigger when cell is swiped in either direction)didSelectRowAt indexPath 在点击时不会(但会在单元格向任一方向滑动时触发)
【发布时间】:2017-07-16 02:55:44
【问题描述】:

这是一个奇怪的(但应该很简单)。我只是想点击一个自定义 UITableViewCell 并触发一个 segue 去另一个 ViewController。 didSelectRowAt indexPath 在点击单元格时不会触发,但奇怪的是会在单元格从右向左滑动或从左向右滑动时触发。

我的didSelectRowAt indexPath:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
   print("You selected cell number: \(indexPath.row)!") 
   self.performSegue(withIdentifier: "mySegue", sender: self)
}

(如建议here)

我的视图控制器:

import UIKit

class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate {

    var myData = NSDictionary()

    override func viewDidLoad() {
        super.viewDidLoad()
        fetchMyData()

        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.rowHeight = 100
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func fetchMyData() {
        // ... fetches my data
    }


    ////////////////////////////////////////////////
    //Table view data source


    //set number of sections in table
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    //set number of rows in table
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myData.count
    }

    //delegate information to cells in table rows
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        print("running tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell...")
        // Dequeue cell
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! myCustomCell

        cell.cellAmount!.text = "Hello World"
        cell.cellTextField2!.text = "Some info"
        cell.cellTextField3!.text = "Some other info"
        cell.backgroundCardView.backgroundColor = UIColor.red
        cell.backgroundCardView.layer.cornerRadius = 8.0
        cell.backgroundCardView.layer.masksToBounds = false
        cell.backgroundCardView.layer.shadowColor = UIColor.black.withAlphaComponent(0.9).cgColor
        cell.backgroundCardView.layer.shadowOffset = CGSize(width: 0, height: 0)
        cell.backgroundCardView.layer.shadowOpacity = 0.9

        print("finished tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell...")
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("You selected cell number: \(indexPath.row)!")
        self.performSegue(withIdentifier: "mySegue", sender: self)
    }

    //    func tableView(_ tableView: UITableView,  editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    //        let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
    //            // delete item at indexPath
    //        }
    //        let share = UITableViewRowAction(style: .normal, title: "Disable") { (action, indexPath) in
    //            // share item at indexPath
    //        }
    //
    //        share.backgroundColor = UIColor.blue
    //
    //        return [delete, share]
    //    }

    func tableView(_ tableView: UITableView,  editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        return []
    }

    // Reload the table data when a change is made
    //    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
    //        self.tableView.reloadData()
    //    }

    /*
     // Override to support conditional editing of the table view.
     override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */

    /*
     // Override to support rearranging the table view.
     override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

     }
     */

    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */


    //Table view data source (end)
    ////////////////////////////////////////////////
}

我也已经尝试过以下方法:

有谁知道在点击单元格时可能导致didSelectRowAt indexPath 不被触发的原因是什么?

【问题讨论】:

  • FWIW:我有一个类似的problem。最终,我错误地设计了 xib,但直到我从 iOS 9 迁移到 iOS 10 时才意识到。
  • 感谢分享@AgRizzo。我仔细检查了 UITableViewCell 在 UITableView 内。我还更新了问题,因为我刚刚注意到向任一方向滑动都会触发didSelectRowAt IndexPath,但点击仍然不会
  • 你是从视图控制器还是从 tableview 启动了 segue。我的意思是在故事板上。你从 tableview 拖放了吗?
  • @DarkInnocence 我尝试将 segue 从单元格(在情节提要上)拖动到另一个视图控制器,并尝试从一个视图控制器(包含 tableview)拖动到目标视图控制器。我还在didSelectRowAt IndexPath 中添加了print 语句,以查看是否调用了t,但点击单元格时未触发segue。点击单元格时根本不会触发它。虽然它被触发并在滑动单元格时执行 segue
  • 感谢分享,@erenkabakci!我查看了我的项目的 XCode 中的 View Debugging 部分(非常酷的东西!),看起来在我的自定义 UITableViewCells 之上没有视图可能正在接收触摸而不是我的单元格。奇怪的是,我重建了整个 ViewController、UITableView 和 UITableViewCell(诚然是出于沮丧),新的工作没有问题。相同的代码、相同的设计、相同的设置。我的好奇心真的很想知道是什么原因造成的第一次出现这个问题,但我很高兴它现在至少在逻辑上有效!

标签: swift uitableview uitableviewrowaction


【解决方案1】:

我遇到了同样的问题并找到了适合我的答案here。我在 viewWillAppear 中有这个键盘关闭代码:

self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

评论此行后,点击开始工作!但现在我必须找到更好的键盘关闭方法。 :) 请检查您的扩展,可能您使用了类似的键盘关闭解决方案。

【讨论】:

  • 啊,抱歉。我错过了这个问题很老了,但谁知道呢,可能会对其他人有所帮助。
猜你喜欢
  • 1970-01-01
  • 2018-01-04
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多