gpt4 book ai didi

swift - didSelectRowAt indexPath 在点击时不触发(但在向任一方向滑动单元格时触发)

转载 作者:行者123 更新时间:2023-11-28 08:05:51 29 4
gpt4 key购买 nike

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

我的 View Controller :

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 在点击单元格时未被触发的原因吗?

最佳答案

我遇到了同样的问题并找到了适用于我的答案 here .我在 viewWillAppear 中有这段键盘关闭代码:

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

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

关于swift - didSelectRowAt indexPath 在点击时不触发(但在向任一方向滑动单元格时触发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45124540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com