DidSelectItemAtIndexPath not called for UITableView inside UIScrollView - ios

My self.view has a UIScrollView as a subview. This UIScrollView has a container UIView, which holds many UITableView's as subviews. These subviews are all configured using autolayout constraints.
So basically my scrollview is horizontally scrollable. I also have UILongPressGesture inside my UIScrollView.
My problem is didSelectItemAtIndexPath is not called while tapping over the cells inside UITableView. I have set cancelTouchesInView to NO for my UILongPressGesture but still didSelectItemAtIndexPath doesn't get called.
I don't want to add UITapGestureRecognizer to my UIScollView and manually trigger didSelectItemAtIndexPath , since I'm doing some operations in UITableView delegate methods.
How can I make my UITableView to responds to all of its delegate methods ?

Related

iOS - UITapGestureRecognizer not fired from UIView inside UIScrollView inside UITableViewCell

I have a custom UIView subclass that has a UITapGestureRecognizer attached to it.
I am trying to use this custom view inside of a UIView (container) inside of a UIScrollView, inside of a UITableViewCell.
So:
UITableViewCell
•UIScrollView
••UIView (container for proper contentSize on UIScrollView)
•••Bunch of UIViews with UITapGestureRecognizer (subclass)
It seems that if I don't use a 'container' view inside the UIScrollView, the taps get detected just fine. But then the contentSize is not correct and some of my custom views inside the UIScrollView are off screen and can't be accessed.
If I do use a 'container' view inside the UIScrollView, the contentSize for the scroll view is correct, but none of the custom view taps get detected.
I have tried all sorts of variations of the below with no success:
tapGesture.cancelsTouchesInView = false
scrollView.canCancelContentTouches = false
scrollView.userInteractionEnabled = true
containerView.userInteractionEnabled = true
Well I couldn't get it to work for some reason, even tried subclassing UIScrollView and the UIView for the container view.
I ended up just adding a UICollectionView to the subclassed UITableViewCell, and then setting the UITableViewController as the UICollectionViewDataSource and UICollectionViewDelegate to handle item selection.

Does UITableViewCell accessoryView has superview?

I assigned a UIView to a UITableViewCell's accessoryView then when i try to get the superView of it seems it's nil?
So does accessoryView has a superview? If no how does it appears?
A view cannot appear on screen unless it has a superview (or is itself a UIWindow).
You may be checking the superview property before it has been set. The accessory view is added as a subview of the cell by the cell's layoutSubviews method. It doesn't happen immediately upon setting the cell's accessoryView property.
If you replace an accessory view with another accessory view, the old accessory view will be removed from its superview (the cell) immediately (inside the setAccessoryView: method), but the new accessory view won't be added to the cell until the cell's layoutSubviews runs.

Make UIView under a UITableView visible when you scroll

I have a UITableView that's placed on top of a UIView.
So basically, it looks like this:
I have a tableviewHolder which has two subviews
1.) UIView with other subviews
2.) UITableView
Whenever the user scrolls the UITableView, i want the UIView beneath it to be visible.
I tried setting the UITableView's background to clear and it works. However, if the tableview's background is clear, then other portion of the UIView beneath it would be visible when the rows of the cell are not enough to cover the whole table.
How can I change the background color of the table view and still make the UIView beneath it visible whenever the user scrolls?
Use
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
(ref https://developer.apple.com/library/ios/documentation/uikit/reference/UIScrollViewDelegate_Protocol/Reference/UIScrollViewDelegate.html#//apple_ref/occ/intfm/UIScrollViewDelegate/scrollViewWillBeginDragging: )
This method will be called when the UITableView begins scrolling. So, in this method, set the UIView to visible
view.setAlpha=1.0;
and in
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
set the uIView to invisible again by using
view.setAlpha=0.0;
Am asking you to use UIScrollViewDelegate methods in a UITableViewDelegate class. This will work because UITableViewDelegate inherits from UIScrollViewDelegate. So all methods in UIScrollViewDelegate will also work for UITableViewDelegate.

UIScrollview in a Custom UICollectionViewCell Not Working

I'm trying to get a UIScrollView to work correctly inside of a UICollectionViewCell.
The custom cell is being loaded in via a xib file and is a subclass of UICollectionViewCell. I had problems getting other controls working, such as a button and a gesture recognizer since the UICollecitonView doesn't seem to be passing any touches to the cells, but I got around those with gesture recognizers on the UICollectionView itself. The one remaining issue I have is the UIScrollViews...
The UICollectionView scrolls horizontally, and the UIScrollView in the cells scroll vertically. I've tried using a UIPanGestureRecognizer to scroll them, but that seems to disable the UICollectionView's ability to scroll.
Anybody have any thoughts?
EDIT: Got it!
So I had converted to a collection view from a previous third party library we were using before iOS6. Turns out the problem was with the xib files we were using for the cells. With the library before, the cells were just subclasses of UIViews. I changed the classes to subclass UICollectionViewCell, and updated the Custom Class. Turns out this was not enough. In order for touches to get passed to the cells I needed to actually had to drag in a new UICollectionViewCell from the Object library, copy over all the subviews and reconnect the IBOutlets. After this, it worked!
I fixed this in my code by making sure resizing of the scroll view happens on main thread.
My collection view is using nsfetchedresultscontroller that is using block calls to refresh selected cells. On the first time a cell was selected the scrollview would not scroll. However if you clicked on another cell and clicked back it would work fine. The initial load of the cell seemed like size calc might not be where it needed to be on main thread to affect behavior.
-(UICollectionViewCell *) collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"CustomCell" forIndexPath:indexPath];
dispatch_async(dispatch_get_main_queue(), ^{
// resizeScrollViewMethod should be where scrollview content size > scroll view frame.
[cell resizeScrollViewMethod];
});
return cell;
}
- (void) resizeScrollViewMethod {
//Do your scrollview size calculation here
}
No problem with UISCrollView in UICollectionViewCell. Use StoryBoard and you'll scroll OK.
UIScrollView overlay UICollectionViewCell, so that didSelect work only when tap outside ScrollView and inside Cell (scrollView.frame < cell.frame).
If you want to implement tap or other gesture, just add it to UIScrollView in awakeFromNib
Refer code:
https://github.com/lequysang/github_zip/blob/master/CollectionViewWithCellScrollViewStoryBoard.zip

UITableView in UIView in UIScrollView. When scrolling tableview don't scroll scrollview too

I have a UITableView inside a UIView inside a UIScrollView.
Problem: when i scroll/drag the tableview, the scrollview scrolls too.
What i need is for the tableview not to pass the scroll to the scrollview. Also i need to be able to use the scrollview when i scroll it directly.
How can i do this?
Cheers
I fixed it using "hitTest" on the tableview.
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
If the event originated within the tableview, i disable the parent's scrollview scroll.
And, when the tableview's scroll has ended (scrollViewDidEndDragging) i re-enable the parent's scrollview scroll.
This seams to work fine.
Set a delegate to your tableview's scrollview (i.e your view controller)
tableView.scrollView.delegate = self;
then use those two calls
– scrollViewDidScroll:
– scrollViewDidEndDragging:willDecelerate:
to disable and re-enable your outside scrollview's scrollEnabled property

Resources