2016-11-21 14 views
0

UICollectionViewが埋め込まれたUITableViewがあります。最初のセクションにはUITextFieldのカスタムセルがあり、2番目のセクションにはUICollectionViewがあります。UITableViewのUICollectionViewは、view.addGestureRecognizer()のためにタップを検出しません。

ユーザーはそれのために私が

let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard)) 
view.addGestureRecognizer(tap) 

func dismissKeyboard(sender: UIGestureRecognizer) { 
    view.endEditing(true) 
} 

を使用していますUITextFieldの外でタップしたときに、キーボードを非表示にするには、私が考え出してきたように、私のUICollectionViewは、その項目のタップを認識しないこと。削除した場合view.addGestureRecognizer(タップ)すべて正常に動作しますが、キーボードはこの場合は非表示になりません。

func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
    textField.resignFirstResponder() //if desired 
    return true 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    switch indexPath.section { 
    case 0: 
     let cell = tableView.dequeueReusableCell(withIdentifier: "ProfileTableViewCell", for: indexPath) as! ProfileTableViewCell 

     cell.value.delegate = self // value is my textField 
     return cell 

    case 1: 
     let cell = tableView.dequeueReusableCell(withIdentifier: "WallpaperTableViewCell", for: indexPath) as! WallpaperTableViewCell 
      cell.setCollectionViewDataSourceDelegate(dataSourceDelegate: self, index: (indexPath as NSIndexPath).row) 
     return cell 

    default: 
     let cell = UITableViewCell() 
     return cell 
    } 
} 

どのようにこの問題を修正し、隠れキーボードを一緒に使用し、UICollectionViewで認識をタップすることができますか? 感謝のUITableViewで、私はフィリップ・ミルズ

https://github.com/DahanHu/DHCollectionTableView

UPD

おかげで、この例を使用していたUICollectionViewを埋め込むには

は、答えはかなりの最も簡単な

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 

ました
func keyboardWillShow(notification: NSNotification) { 
    view.addGestureRecognizer(tap) 

} 

func keyboardWillHide(notification: NSNotification) { 
    view.removeGestureRecognizer(tap) 
} 

答えて

0

キーボードが表示されている場合にのみジェスチャ認識器を追加し、キーボードを閉じたときにジェスチャ認識器を削除します。

関連する問題