【发布时间】:2018-07-31 12:11:33
【问题描述】:
下午好。 我正在创建一个简单的小应用程序,并且我有一个简单的“功能”,我无法弄清楚如何实现,对编程来说相当陌生。我有一个 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
}
感谢您阅读我的帖子!
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell didselectrowatindexpath