我正在实现滑动以删除UiTableView的操作,我创建了一个表格,它工作正常。但是,当我点击表视图中的项目时,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]
}
}
可以帮助我解决这个问题。
更新
当我从左向右滑动时,该方法有效。不是项目点击
答案 0 :(得分:0)
检查以下列表。
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
}
b)当你点击删除按钮时,这个功能被称为
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if(editingStyle == UITableViewCellEditingStyle.delete)
{
print("Delete Pressed")
}
}
c)现在,当您看到删除按钮然后点击单元格而不是删除按钮本身时,删除按钮将隐藏,并且第一次不会调用任何方法。
现在隐藏了删除按钮,您可以轻松点按该单元格。你去了,现在你的下面的函数被称为
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("didSelectRowAt(\(indexPath)")
}
除此之外,如果你想要任何其他行为。然后,您必须自定义UITableViewCell。
如果您遇到任何其他问题,请仔细检查您的实施。
答案 1 :(得分:0)
我找到了解决方案
在viewDidLoad中执行此操作
let tapOnScreen: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ManageDevicestable.CheckTheTime))
tapOnScreen.cancelsTouchesInView = false
managetable.addGestureRecognizer(tapOnScreen)
添加选择器方法
func CheckTheTime() -> Void {
print("tapped")
}