2017-10-05 74 views
-1

我正在實施一個滑動刪除UiTableView的行動,我創建了一個表,它工作正常。但是當我點擊表格視圖中的項目時didSelectRowAt未被調用 - 它不打印日誌。UiTableView didSelectRowAt方法不叫

enter image description here

代碼

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。

更新

當我刷卡由左到右的方法工作。不在物品上點擊

+0

有你設置的tableview委託ManageUsersTable在其情節提要/筆尖? –

+0

是的。我已經設置了代理 –

+0

驗證'userInteractionEnabled'屬性一次。 – Buntylm

回答

0

我找到了解決辦法

在您的viewDidLoad

let tapOnScreen: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ManageDevicestable.CheckTheTime)) 
     tapOnScreen.cancelsTouchesInView = false 
     managetable.addGestureRecognizer(tapOnScreen) 

做這個加法選擇方法

func CheckTheTime() -> Void { 
     print("tapped") 
    } 
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)刪除刷卡的默認行爲如下

一)當你滑過單元格 - >你會看到一個刪除按鈕。

該功能被稱爲

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。

如果您遇到任何其他問題,請徹底重新檢查您的實施。