gpt4 book ai didi

ios - Swift:使用 didSelectItemAt indexPath 函数仅选择单元格,即使我点击其中的 UI 元素也是如此

转载 作者:行者123 更新时间:2023-11-30 11:17:26 25 4
gpt4 key购买 nike

下午好。我正在创建一个简单的小应用程序,并且我想到了一个简单的“功能”,但我不知道如何实现,因为我对编程相当陌生。我有一个 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}

}

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

问题:我想添加,以便当我点击单元格单个框架内的任何位置时,即使我点击 UITextView (此时如果我点击 TextView ,它也会选择它)进入该 View 的编辑模式)。如果我第二次点击它,我希望能够编辑 TextView 。 - 我该如何实现这个?

单元格创建代码:

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")
}
}

然后在类中我创建 UICollectionView:

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

return cell
}

感谢您阅读我的帖子!

最佳答案

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:第一次选择您的单元格时,由于我们在步骤 (3) 中所做的操作,您的 textView 将无法选择,第一次点击后,我们将启用第二次触摸的 textView

2:每当我们在编辑 TextView 后关闭键盘时,我们都会将交互设置为 false,以便我们能够选择点击的单元格而不是 TextView

关于ios - Swift:使用 didSelectItemAt indexPath 函数仅选择单元格,即使我点击其中的 UI 元素也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51613110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com