didSelectRowAt索引路径在点击时不会(但在向任一方向滑动单元格时会触发)

时间:2017-07-16 02:55:44

标签: swift uitableview uitableviewrowaction

这是一个奇怪的(但应该很简单)。我只是尝试点击自定义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

我的ViewController:

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未被触发的原因?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并找到了适用于我的答案here。我在viewWillAppear:

中有关于键盘关闭的代码
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self.view, action: #selector(UIView.endEditing(_:))))

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