Ios 未调用UiTableView didSelectRowAt方法
我正在为UiTableView实现一个滑动删除操作,我创建了一个工作正常的表。但是,当我在表视图中单击项目时,不会调用Ios 未调用UiTableView didSelectRowAt方法,ios,uitableview,swift3,Ios,Uitableview,Swift3,我正在为UiTableView实现一个滑动删除操作,我创建了一个工作正常的表。但是,当我在表视图中单击项目时,不会调用didSelectRowAt——它不会打印日志 代码 class ManageUsersTable: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var editView: UIView! @IBOutlet weak var main_user_t
didSelectRowAt
——它不会打印日志
代码
class ManageUsersTable: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var editView: UIView!
@IBOutlet weak var main_user_table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
editView.isHidden = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("UsersTableCell", owner: self, options: nil)?.first as! UsersTableCell
cell.selectionStyle = UITableViewCellSelectionStyle.none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("selected : \(indexPath.row)")
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
print("Delete at index : \(indexPath.row)")
let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
// delete item at indexPath
print("Delete at index : \(indexPath.row)")
self.editView.isHidden = false
}
// let edir = UITableViewRowAction(style: .normal, title: "Edit", icon : UIImage() ) { (action, indexPath) in
// print("Edit at index : \(indexPath.row)")
// }
// edit.backgroundColor = UIColor.darkGray
// return [delete, edit]
return [delete]
}
}
有人能帮我修一下这个tnx吗
更新
当我从左向右滑动时,该方法有效。不在项目上单击检查以下列表 1) 不要忘记设置UITableViewDelegate或UITableViewDataSource 2) 请使用更新viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
editView.isHidden = true
main_user_table.allowsMultipleSelectionDuringEditing = false //UPDATE THIS LINE
}
3) 删除滑动的默认行为如下所示
a) 当您滑动单元格->时,您将看到一个删除按钮
此函数称为
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
print("swipe")
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == UITableViewCellEditingStyle.delete)
{
print("Delete Pressed")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt(\(indexPath)")
}
b) 当您单击删除按钮时,此功能被调用
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
print("swipe")
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == UITableViewCellEditingStyle.delete)
{
print("Delete Pressed")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt(\(indexPath)")
}
c) 现在,当您的delete按钮可见,然后点击单元格而不是delete按钮本身时,delete按钮将隐藏,并且第一次不会调用任何方法
现在删除按钮被隐藏,您只需点击单元格。现在调用下面的函数
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
print("swipe")
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == UITableViewCellEditingStyle.delete)
{
print("Delete Pressed")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt(\(indexPath)")
}
除此之外,如果你想要任何其他行为。然后必须自定义UITableViewCell
如果您遇到任何其他问题,请彻底重新检查您的实施。我找到了解决方案
在您的视图中执行此操作
let tapOnScreen: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ManageDevicestable.CheckTheTime))
tapOnScreen.cancelsTouchesInView = false
managetable.addGestureRecognizer(tapOnScreen)
添加选择器方法
func CheckTheTime() -> Void {
print("tapped")
}
是否已在其脚本/nib中将tableview委托设置为ManageUsersTable?是。我已经设置了delegateVerify
userInteractionEnabled
属性一次。您确定您的tableview选择是单一选择吗?还有编辑,你为编辑设置了什么?