greg
greg

Reputation: 4953

Can't scroll both UIScrollView in UITableViewCell and the UITableView itself

My pure AutoLayout UITableViewCell looks like this in Interface Builder:

UITableViewCell
|-> UITableViewCell.contentView
    |-> UIView (ScrollViewContainerView)
        |-> UIScrollView
            |-> left (fixed)
            |-> center (fill remaining)
            |-> right (fixed)

The UIScrollView contains a left, center, and right UIView. left and right are both fixed width, while center expands to fill the remainder of the UIView. The UIScrollView constraints are to align all edges to ScrollViewContainerView. ScrollViewContainerView constraints are to align all edges to the UITableViewCell.contentView. I have a constraint on center's width to be a multiple of ScrollViewContainerView's width, so the UIScrollView scrolls left and right, but the height is fixed and does not scroll. Note that the UIScrollView has been subclassed to include this code so that the UITableView can detect a tap on the cell to toggle selection.

The issue is that I currently can either scroll the UITableView containing these UITableViewCells up and down or I can scroll the UIScrollViews in the UITableViewCells left and right, not both.

When ScrollViewContainerView.userInteractionEnabled == YES, I can't scroll the UITableView up and down, but I can scroll the UIScrollView left and right. When ScrollViewContainerView.userInteractionEnabled == NO, I can scroll the UITableView up and down, but I can't scroll the UIScrollView left and right. userInteractionEnabled == YES on everything else in the above hierarchy.

I can get away with having ScrollViewContainerView as a sibling view to the UIScrollView (making the UIScrollView the direct descent of contentView -- can't get rid of this view completely, because I require it to get the dimensions for the UIScrollView frame). In that case, the opposite handling with userInteractionEnabled holds.

I know I've done this before in other projects before, but starting fresh again, I can't seem to figure out what step I'm missing. Currently using Xcode 6 6A215l targeting iOS 8, though I have reproduced the issue under Xcode 5 targeting iOS 7.

Upvotes: 1

Views: 2835

Answers (3)

Rainer Liao
Rainer Liao

Reputation: 223

I met some similar problem.

I have a scrollView in tableViewCell. All works fine.

Until one day, someone told me that the tableView can't scroll up/down when finger is touched on the scrollView in 6p. Just in 6p, not in 5, 5s,or6.

This makes me almost crazy.

Finally, I set the scrollView's height smaller than the height in storyboard.

Biu ~ It works~~~

Still, I don't know why.

Upvotes: 1

greg
greg

Reputation: 4953

@user2277872's answer put me on the right track to look at the output of the UIScrollView delegate methods of the UIScrollView in my UITableViewCell subclass. Putting an NSLog() in scrollViewWillBeginDragging: made me notice that the UIScrollView was receiving scrolling events while I was trying to scroll the UITableView. My UIScrollView had a contentSize larger than its frame in both directions, but I've forced that view to only scroll horizontal, so ignored the height and reset it. That force was my undoing and I should have known it at the time -- the correct solution is to fix the frame height. If the UIScrollView doesn't think there is more vertical content, it will correctly forward the swipe up/down gesture to the UITableView.

While I attempt to figure out why my contentSize is too large when it wasn't before (thinking I'm missing a clipToBounds somewhere), what I'm doing to force horizontal scrolling temporarily is (in the UITableViewCell's subclass):

- (void)drawRect:(CGRect)rect
{
        [super drawRect:rect];

        CGSize contentSize = self.scrollView.contentSize;
        contentSize.height = self.frame.size.height;
        self.scrollView.contentSize = contentSize;
}

EDIT: Actually, this is seemingly better than overriding drawRect. This would be in the UIScrollView subclass:

/*
 * Lock to horizontal scrolling only.
 */
- (void)setContentSize:(CGSize)contentSize
{
    [super setContentSize:CGSizeMake(contentSize.width, 1)];
}

The height struct member isn't too important, as long as it's guaranteed to be smaller than the frame.size.height of the UITableViewCell. Still hacky, still need to find why I could clip before and not now.

Upvotes: 0

user2277872
user2277872

Reputation: 2973

It sounds like the scrollview is causing your tableview to not allow userInteraction when being scrolled. I'm sure that if you called - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView in the UIScrollView delegate (not sure for iOS 8), but you could just do

 - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
         if(scrollView.dragging == YES) {
            self.<scrollViewName>.userInteractionEnabled = NO;
         }
 }

This is untested code, but it's just a bit of help to get you where you need to go.

Hope it helps!

Upvotes: 1

Related Questions