下午好。 我正在创建一个简单的小应用程序,并且我有一个简单的“功能”,我无法弄清楚如何实现,这对编程来说是个新手。我有一个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}
}
单元格内容:有点长,但是如果有要求,我将发布代码(以编程方式创建的单元格)。但基本上我在每个单元格中都有一些UIImageViews,两个UITextViews,一个UITextLabel和一个UIButton。 (因此,单元格是在单独的类中声明的,而不是在UICollectionViewDelegate和DataSource的类中声明的。)
问题:我想添加一个内容,以便当我在单元格中的任意位置点击单个框架时,即使我点击UITextView,我也希望选择它(此刻,当我点击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
}
感谢您阅读我的帖子!
答案 0 :(得分:0)
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