UIScrollView inside UITableViewCell touch detect

后端 未结 5 1480
清歌不尽
清歌不尽 2020-12-04 20:16

I have a tableview with 8 custom cells. in the 8th cell I added a scrollView with paging enabled so I can show page 1 and page 2 (or 3, 4... 10) without have a very high cel

5条回答
  •  隐瞒了意图╮
    2020-12-04 20:35

    The selected answer is correct, but I updated the code based on a bug I was getting.

    In the subclassed scroll view add the following code.

    - (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];
        }
    }
    

    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.

提交回复
热议问题