Event based methods of my delegate are not called while others are called. For example, willDisplayCell is called, but shouldHighlightItemAt and shouldSelectItemAt are not called. I overwrote point(inside:...) method inside my custom UICollectionView, and it gets called and super returns true. What could be causing this? I'm on iOS 11.4, Swift 4.1, and Xcode 9.4.1.
I have added a supplementary view to the collection view that spans the entire area of its bounds. This view was intercepting all tap events. Once I set its isUserInteractionEnabled to false I started receiving highlight and selection calls on cells in my delegate.
Related
I have a weird question regarding NSCollectionView. Basically, I have a collection view that scrolls horizontally in a vertically scrolling table view. I have implemented the data source and delegate of the collection view within my NSTableCellView subclass. The data source works just fine and the collection view is able to load some images.
Here comes the problem. I want the collection view to be selectable. I have implemented this delegate method into my table cell view subclass:
func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
// Do things here
}
This method works, but with a caveat. It only gets called when I click on an empty space in the collection view first and then selecting a cell. If I only click on a cell, it will not get called, which is quite bizarre to me. Any idea on how to fix this?
I designed the table cell view in the storyboard, the collection view has it's Selectable and Allow Empty Selection checked in the storyboard. Allow Multiple Selection is not checked because I don't want multiple selection, but I tried having it on and it doesn't change anything.
Thanks
Subclass NSTableView. In your subclass, override validateProposedFirstResponder(_:for:) to return true for your collection view and its subviews (or maybe just return true always).
I have a main UICollectionView which contains a custom header and a custom cell. Inside the custom cell I have another UICollectionView with a custom cell in which there are three buttons (inside a stackView) that has added target as a selector function to it for .touchUpInside but when I touch the button it does not fire the selector function at all. didSelectItemAt function gets called on parent UICollectionView but not the nested one. If I comment out the parent didSelectItemAt nothing is called.
I have also set the nested collectionView delegate. Any idea what I should do to make it work would be much appreciated.
(I don't use storyboard)
Check that view.isUserInteractionEnabled is true for the parent view.
Try to disable your parent collection view's .delaysContentTouches, and if that doesn't work, try to override hitTest all the way down at your button's view level hierarchy and see if the touch reaches it.
I have imported UICollectionViewDelege, UICollectionViewDatasource and UIScrollViewDelegate.
I have set my collection views delegate and datasource to self.
But scrollViewDidScroll isnt being called, my scroll view goes horizontal if that makes a difference?
Why is scrollViewDidScroll not being called if collection views use the scrollview inside them?
I was moving form swift 2 to swift 3 and what did the trick is the following:
swift 2:
public func scrollViewDidScroll(scrollView: UIScrollView)
to swift 3:
public func scrollViewDidScroll(_ scrollView: UIScrollView)
So basically the "_" underscore did the trick. What is annoying is that swift compiler didn't complain about it!!!
As for inheritance UICollectionViewDelegate should do the trick.
I had to add information that my controller conforms to UIScrollViewDelegate protocol in order to make it work
I've also encountered this issue. I started off by setting the delegate to self inside of storyboard, at that point scrollViewDidScroll wasn't getting invoked (but functions like collectionView willDisplayCell were, as expected). After some playing around I've noticed that setting delegate programmatically fixed the issue.
I have a tableView with custom cells. Each custom cell has a textfield. When a user clicks on a textfield, I want certain new cells to appear on the screen.
This is what I'm trying:
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
/*
...code to determine which new cells should be added in from my data structure
*/
self.tableView.reloadData()
return true
}
However, every time reloadData is called, I think the delegate calls textFieldShouldBeginEditing again, so I get stuck in an infinite loop and the method never returns.
Any ideas on how I can avoid this problem?
I think what you are looking for is the insertRowsAtIndexPaths call instead of reloading the whole table.
func insertRowsAtIndexPaths(_ indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation)
another stack overflow post you should look at
here's the link to the Apple docs
keep in mind you when doing this you need to keep your datasource up to date as well. update the datasource before calling insertRowsAtIndexPath
I am drawing custom UITableViewCells. My cells are opaque and are completely drawn in the drawRect of the cell to help with performance. I want to handle the look of a selected cell myself. If I don't, then the contents of my cell is covered up by the selectedBackgroundView that is added. Is it common or acceptable to override the setSelected:animated method of my cell so this is done properly. I guess if I did that, then I would not call the super's setSelected method since I would be handling how the cell will show that its selected. I would also have to set the selected property of the cell.
Any help would be great. Thanks.
You can override -[UITableViewCell setSelected:animated:], but you should always call the super's implementation in your implementation. Not doing so could have have unanticipated effects on other selection-related behaviors.
If you don't want the superclass to make any styling changes as a result of the selection, just set the cell's selectionStyle property to UITableViewCellSelectionStyleNone.