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.
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 UICollectionView with horizontal scrolling,i can't add a UICollectionViewCell to my class.
After adding a Button to the cell , I was able to add a reference to the button in the cell.
My conception to add reference to an empty cell was wrong.
One of my view controllers contains a UITableView with custom UITableViewCell's. My custom UITableView cell contains a UIImageView subview that represents a tappable icon. When the user taps anywhere on the custom cell, except the icon subview, I want my didSelectRowAtIndexPath method to be called like normal. But when my icon subview gets tapped, I want a different method to get called, but I can't figure out how to do this. Do I have capture the touch position in the touchesDidBegin method and manually check if the user tapped the icon? That just feels so hacky. Other, cleaner ideas?
Thanks so much for your wisdom!
Simply add a tapGestureRecognizer to the UIImageView in your cell
I've got a UITextView inside a UITableViewCell subclass. I have no problem getting the new height of the Text view and cell. The problem I have is telling the UITableView to update.
I have implemented heightForRowAtIndexPath: to return the live height of the cell as the TextView expands.
But somewhere `[tableView beginUpdates]; [tableView endUpdates]; must be called.
How? Should I add a delegate property to the UITableViewCell which is set to the UITableViewController subclass? And then send a delegate message when the cell expands height and the Tableview needs to update? It seems a little weird to have a delegate between the UITableViewCell and Controller?
I tried using NSNotificationCenter, but I have more than one editable cell, and more than tableview of this nature. So there is no way to register only for notifications for the cells without copying and pasting the same line over again, which isn't nice (as the cells are created in IB, and are not in an array or set), and having multiple tableviews means an exception occurs on the other table view as it is told to update but nothing changes.
I've seen lots of questions and answers on this topic, but when it comes to updating the tableview they all just say "now update the tableview" and not how to. So how do I telly he tableview to update, from one of it's cells?
I would think that this behavior would be best implemented in the UITableViewController instead of the view itself (the UITableViewCell).
Your controller is responsible for setting cell height, and typically will be the delegate for your UITextView's, so let it handle all of this.
In your textViewDidChange method, figure out what the new height of your cell should be, update your data structure to reflect that, and call reloadRowsAtIndexPaths:withRowAnimation: to have it actually change.
Edit:
So since you didn't like my first suggestion, another way to do this would be to add a recommendedRowHeight property to your custom UITableViewCell.
Then, you can either observe this property from your UITableViewController or implement a delegate protocol with a method along the lines of:
- (void)recommendedRowHeightDidChange
// or
- (void)recommendedRowHeightDidChangeTo:(CGFloat)newHeight
Then, when your height changes, update your recommendedRowHeight property and call your delegate's method if you go that route.
Either way, once your controller figures out that the recommended row height of a cell has changed, it can do what it is supposed to do. Update your data structures reflecting the current row heights and then call reloadRowsAtIndexPaths:withRowAnimation:.
You can add your tableview controller object as a weak reference to your tableview cell class. And in tableview controller you can have a method which will be called from tableview cell class.
How can I use touchesbegan in a table's cell without having to subclass a whole cell. Something like addTarget..... which is available for a UIButton?
(in vb.net this would be like AddHandler I think)
You must subclass UITableViewCell in order to have access to individual touches on a tableview cell.
You cant do it, you will have to subclass UITableView cell just like the post before me said, then you can add some view in the cells content view there which can delegate the touches to the tableviewcontroller or wherever they need to go...
#Jaco Relkin, you can implement the messages on the UITableViewDelegateProtocol, for example: the message tableView:didSelectRowAtIndexPath: documented here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intfm/UITableViewDelegate/tableView:didSelectRowAtIndexPath: