1

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.

Community
  • 1
  • 1
greg
  • 4,843
  • 32
  • 47
  • What iOS are you using? I am doing currently something similar in a project and it works on 7. I was even able to scroll vertically the scroll view, when I messed up the vertical constraints and the content size got bigger than the frame in height. – Levi Jun 07 '14 at 19:25
  • @Levi My previous projects targeted iOS 7 and worked. I'm re-implementing this on iOS 8 now. – greg Jun 07 '14 at 19:56
  • Woah woah woah! Why are you using a scrollview inside a tableview cell? – Lord Zsolt Jun 07 '14 at 19:59
  • Could you try and run it on an iOS 7 device and let me know how it goes? Because if this doesn't work because of the OS version, I am going to have a problem too – Levi Jun 07 '14 at 20:53
  • @LordZsolt How else would you display a content that needs to be laid out horizontally and exceeds the width of the cell? – Levi Jun 08 '14 at 08:32
  • @Levi same situation under iOS 7 via Xcode 5. I'm sure this *should* work, I'm just blocking the responder chain somewhere along the line and am not sure where. – greg Jun 08 '14 at 13:06

3 Answers3

1

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!

user2277872
  • 2,963
  • 1
  • 21
  • 22
1

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.

Rainer Liao
  • 223
  • 3
  • 13
  • "Finally, I set the scrollView's height smaller than the height in storyboard." this sentence save my life :) it really works. I have smiliar issue. And I see your answer and problem solved. Thanks. – ali Dec 24 '15 at 07:14
0

@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.

greg
  • 4,843
  • 32
  • 47