Swift:将 didSelectItemAt indexPath 函数仅用于 select 单元格,即使我点击其中的 UI 元素也是如此

Swift: Using didSelectItemAt indexPath func to only select the cell even if I tap on a UI element within it

下午好。 我正在创建一个简单的小应用程序,我有一个简单的 "feature" 想法,但我不知道如何实现,对编程来说还很陌生。我有一个 UICollectionView,其中的单元格位于另一个 UICollectionView 的单元格中,当我点击其中一个单元格时,它们会触发以下 didselect 和 diddeselect 方法:

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) {

        cell.layer.borderColor = UIColor.flatBlack.cgColor
        cell.layer.borderWidth = 3
        cell.layer.cornerRadius = 7
        cell.backgroundColor = GradientColor(.topToBottom, frame: cell.frame, colors: [UIColor.flatRed.withAlphaComponent(0.2), UIColor.white])


    } else {return}

}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.layer.borderColor = UIColor.clear.cgColor
        cell.layer.borderWidth = 0
        cell.layer.cornerRadius = 0
        cell.backgroundColor = .white
    } else {return}

}

单元格内容:有点长,但如果有人问我会post代码(以编程方式创建的单元格)。但基本上我在每个单元格中有几个 UIImageView、两个 UITextView、一个 UITextLabel 和一个 UIButton。 (因此单元格在单独的 class 中声明,而不是在 class 中声明,后者是 UICollectionViewDelegate 和 DataSource)。

问题: 我想添加,这样当我点击单元格单个框架内的任何地方时,即使我点击 UITextView(此时如果我点击它进入该视图的编辑模式的文本视图)。如果我第二次点击它,我希望能够编辑文本视图。 - 我该如何实施?

单元格创建代码:

class EventsCell: UICollectionViewCell {
  override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor.white
    setupViews()


}
//MARK: - Declaration of cell organelles.
//separatorView for a separating line between cells

let textView: GrowingTextView = {   //GrowingTextView is a pod it is a regular UITextView but with some perks.
    let tv = GrowingTextView()
    tv.backgroundColor = .clear
    tv.allowsEditingTextAttributes = true
    tv.isScrollEnabled = false
    tv.font = UIFont(name: "Didot", size: 16)
    tv.textContainerInset = UIEdgeInsetsMake(4, 4, 4, 6)
    tv.placeholder = "Write your event text here"
    tv.placeholderColor = UIColor.lightGray
    tv.autoresizingMask = .flexibleHeight
    tv.isUserInteractionEnabled = false
    return tv
}()



let eventPlaceholderMarkImage: UIImageView = {
    let iv = UIImageView()
    iv.image = UIImage(named: "placeHolderEventTitleMark")
    return iv
}()

func setupViews() {


    addSubview(textView)
    addSubview(eventPlaceholderMarkImage)


    let eventsPinImageH = frame.width / 2 - 7
    let cellHeight = frame.height
    let cellWidth = frame.width

    //MARK: - Constraints for EventsCell

    addConstraintsWithFormat(format: "H:|-16-[v0(\(frame.width - 32))]-16-|", views: textView)
    addConstraintsWithFormat(format: "V:|-47-[v0]-16-|", views: textView)




    addConstraint(NSLayoutConstraint(item: eventPlaceholderMarkImage, attribute: .bottom, relatedBy: .equal, toItem: textView, attribute: .top, multiplier: 1, constant: -5))
    addConstraint(NSLayoutConstraint(item: eventPlaceholderMarkImage, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 0, constant: 20)) 
    //Followed by much more constraints
    required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}
}

然后在 class 中创建 UICollectionView:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: EventCellID, for: indexPath)

    return cell
}

感谢您阅读我的post!

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) {

        cell.yourTextView.isUserInteractionEnbled = true //1


        cell.layer.borderColor = UIColor.flatBlack.cgColor
        cell.layer.borderWidth = 3
        cell.layer.cornerRadius = 7
        cell.backgroundColor = GradientColor(.topToBottom, frame: cell.frame, colors: [UIColor.flatRed.withAlphaComponent(0.2), UIColor.white])


    } else {return}

}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    if let cell = collectionView.cellForItem(at: indexPath) {
        cell.layer.borderColor = UIColor.clear.cgColor
        cell.layer.borderWidth = 0
        cell.layer.cornerRadius = 0
        cell.backgroundColor = .white
    } else {return}

}

override func viewWillAppear(_ animated: Bool) {        
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear), name: Notification.Name.UIKeyboardWillHide, object: nil)
}


@objc func keyboardWillDisappear() {

    cell.yourTextView.isUserInteractionEnbled = false //2
}

创建单元格时:

...
cell.yourTextView.isUserInteractionEnabled = false // 3

1:第一次 selected 时,由于我们在步骤 (3) 中所做的操作,您的 textView 将无法 select,在第一次点击后我们'为第二次触摸重新启用 textView

2:每当我们在编辑 textview 后关闭键盘时,我们都会将交互设置为 false,这样我们就可以 select 点击单元格而不是 textView