JAHelia
JAHelia

Reputation: 7932

uiscrollview for a custom uitableview cell not scrollable (or accessible)

I have a grouped table view with custom cells in it, and it contains many cells (i.e. scrollable), there is a scroll view in every cell (which contains a UILabel), I've set up that scroll view properly (made its content size larger than its frame size for the scroll to work properly and then added it as a sub-view on the custom cell), however, the text on the label that is inside this scroll view appears but not scrollable (no scroll bars, no scrolling ...), the only scrollable object on the screen is the default scroll view of the grouped table view.

how can i get the mini scroll view to scroll properly ?

thank you in advance.

Upvotes: 0

Views: 1497

Answers (2)

cnotethegr8
cnotethegr8

Reputation: 7510

Here's the full code of what should be written in the subclass of the scroll view.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.dragging) {
        [super touchesBegan:touches withEvent:event];
    } else {
        [self.superview touchesBegan:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.dragging) {
        [super touchesMoved:touches withEvent:event];
    } else {
        if ([self.delegate isKindOfClass:[UITableViewCell class]]) {
            [(UITableViewCell *)self.delegate touchesCancelled:touches withEvent:event];
        }

        [self.superview touchesMoved:touches withEvent:event];
    }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.dragging) {
        [super touchesEnded:touches withEvent:event];
    } else {
        [self.superview touchesEnded:touches withEvent:event];
    }
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    if (self.dragging) {
        [super touchesCancelled:touches withEvent:event];
    } else {
        [self.superview touchesMoved:touches withEvent:event];
    }
}

In the touchesMoved there's some extra code based on a bug I was encountering. To start, if your self.delegate is not the UITableViewCell, than replace that property with a property to your cell.

The cell needs to retrieve the cancel touch event during movement to prevent the undesired results. It can be easily reproducible as follows.

  • Highlight the cell (assuming the scroll view is over the whole cell, if not highlight the scroll view)
  • While the cell is highlighted, drag the table view
  • Select any other cell and now the previously highlighted cell will retrieve the didSelectCell state

Another point to mention is that order matters! If the self.delegate is not called before the self.superview then the highlighted state wont happen.

Upvotes: 0

ader
ader

Reputation: 5393

Are you implementing touchesBegan and touchesEnded in your scrollview?

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event 
{
    [super touchesBegan:touches withEvent:event];
    [[self nextResponder] touchesBegan:touches withEvent:event];
}


- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event 
{
    [super touchesEnded:touches withEvent:event];
    [[self nextResponder] touchesEnded:touches withEvent:event];
}

If not, make a class for your scrollview (scrollview subclass) and implement them therein.

Upvotes: 2

Related Questions