class DemoItem {
}
class DemoCell: UICollectionViewCell {
lazy var textView: UITextView = {
let newTextView = UITextView()
newTextView.textContainerInset = UIEdgeInsetsZero
newTextView.textAlignment = .Center
newTextView.font = UIFont.systemFontOfSize(20)
// newTextView.userInteractionEnabled = false
newTextView.backgroundColor = UIColor.clearColor()
newTextView.scrollEnabled = false
self.contentView.addSubview(newTextView)
return newTextView
}()
override func layoutSubviews() {
super.layoutSubviews()
textView.frame = bounds
textView.text = "Demo Text"
}
}
class DemoSectionController: IGListSectionController, IGListSectionType {
var item: DemoItem
init(item: DemoItem) {
self.item = item
}
func numberOfItems() -> Int {
return 1
}
func sizeForItemAtIndex(index: Int) -> CGSize {
let width = (collectionContext?.containerSize.width ?? 200)
return CGSize(width: width, height: 50)
}
func cellForItemAtIndex(index: Int) -> UICollectionViewCell {
let cell = collectionContext?.dequeueReusableCellOfClass(DemoCell.self,
forSectionController: self,
atIndex: index) as? DemoCell
?? DemoCell()
cell.backgroundColor = UIColor.redColor()
return cell
}
func didUpdateToObject(object: AnyObject) {
self.item = object as? DemoItem ?? DemoItem()
}
func didSelectItemAtIndex(index: Int) {
print("Selected item at index = \(index)")
}
}
this code shows an UITextView inside an IGListSectionController
uncommenting the following line newTextView.userInteractionEnabled = false fires func didSelectItemAtIndex(index: Int) correctly but doesn't show the keyboard, enabling user interactions shows the keyboard but doesn't notify the section controller of item selection.
Is that an unintended use case, or did I miss something during the setup?
As far as I'm aware that's the base functionality internally, UITextViews will intercept the touch action before it gets to the cell due to its ability to be edited.
By turning userInteractionEnabled to false you're telling the textview to no longer accept touch actions and therefore the cell receives it (triggering the didSelectItemAtIndex)
If you're wanting to detect when a user starts to edit a text view (usually by means of tapping the text view, and keyboard popping up) then you will need to do so using a delegate. Add the protocol UITextViewDelegate to your class, assign it to the textView's delegate property and add one of the delegates functions such as textViewDidBeginEditing.
Apologies if I've misinterpreted your issue!
Thanks @Sherlouk ! 馃帀
Also, your SectionController could be the UITextViewDelegate.
@sanboxapps - let us know if this helps 馃槃
Thanks for the help, the initial intention is to have the sectionController recognize which item (I forgot to mention there could be a multitude of them) was tapped so that when the text changes it knows how to handle the change based on the item. I think 2 possible ways around that would be as @Sherlouk mentioned to rely on the text view'a delegate (in my case by wrapping it in another delegate that contains more info about the item being edited), another approach (which I am not a very big fan of) is to have one item per section controller where the controller is also the delegate
So it depends what information you really need, it's possible to get the index of a UICollectionViewCell by using the collectionContext?.index(for cell: UICollectionViewCell, sectionController: IGListSectionController) -> UInt function.
If you were to have the UITextViewDelegate in the cell it self, which then forwards a message onto the section controller (see our RemoveSectionController for an example of this!) containing the cell then you can use the aforementioned function to receive the index.
From that index you should be able to use your own logic and the object passed to the section controller to handle it which way you seem fit
Does that make sense, @sanboxapps?
Maybe a bit of code to show what you're trying to do if it doesn't!
@sanboxapps are we ok to close this issue? Looks like it's answered.
@rnystrom sure
Most helpful comment
So it depends what information you really need, it's possible to get the index of a UICollectionViewCell by using the
collectionContext?.index(for cell: UICollectionViewCell, sectionController: IGListSectionController) -> UIntfunction.If you were to have the
UITextViewDelegatein the cell it self, which then forwards a message onto the section controller (see ourRemoveSectionControllerfor an example of this!) containing the cell then you can use the aforementioned function to receive the index.From that index you should be able to use your own logic and the object passed to the section controller to handle it which way you seem fit
Does that make sense, @sanboxapps?
Maybe a bit of code to show what you're trying to do if it doesn't!