UITableView inside UIScrollView not receiving first tap after scrollling

前端 未结 14 1797
北海茫月
北海茫月 2020-11-29 04:31

Brief

I am having an issue with a UITableView inside a UIScrollView. When I scroll the external scrollView, the table<

14条回答
  •  自闭症患者
    2020-11-29 05:17

    I've encountered a UITableView with scrollEnabled being NO within a UIScrollView in some legacy code. I have not been able to change the existing hierarchy easily nor enable scrolling, but come up with the the following workaround for the first tap problem:

    @interface YourOwnTableView : UITableView
    @end
    
    @implementation YourOwnTableView
    
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    
        [super touchesCancelled:touches withEvent:event];
    
        // Note that this is a hack and it can stop working at some point.
        // Looks like a table view with scrollEnabled being NO does not handle cancellation cleanly,
        // so let's repeat begin/end touch sequence here hoping it'll reset its own internal state properly
        // but won't trigger cell selection (the touch passed is in its cancelled phase, perhaps there is a part
        // of code inside which actually checks it)
        [super touchesBegan:touches withEvent:event];
        [super touchesEnded:touches withEvent:event];
    }
    
    @end
    

    Again, this is just a workaround working in my specific case. Having a table view within a scroll view is still a wrong thing.

提交回复
热议问题