【发布时间】: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)
////////////////////////////////////////////////
}
我也已经尝试过以下方法:
- -didSelectRowAtIndexPath: not being called
- How to detect Cell selection in UITableView - Swift
- Push segue from UITableViewCell to ViewController in Swift
- How to tap cell's buttons in UITableViewCell without actionning the cell's segue
- is it possible to segue from a UITableViewCell on a UIView to another view
- UITableView didSelectRowAt is not called iOS 10, but works for 9.3.5
- UITableViewCell 确实有 UIView 和多个 UITextFields,所以我确实确保
User Interaction Enabled未选中 UIView 和 UITextFields。 -
编辑:我还查看了
debugger navigator,可以看到当我点击一个单元格时 CPU 使用率增加了 ()。也许有一种方法可以更深入地了解为什么该点击不会导致didSelectRowAt indexPath被触发..?
有谁知道在点击单元格时可能导致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