1

I have a UIWebView in my UITableView cell. However when I scroll the UIWebView, UITableView scrolls too. How do I make sure only one UIWebView scrolls when the user tries to scroll it?

cbuchart
  • 10,847
  • 9
  • 53
  • 93
The Cook
  • 1,403
  • 3
  • 17
  • 33
  • Nothing fancy, I just do webview.loadHTMLString(htmlString, baseURL: nil) parent.addSubview(webview) on cell's main view. – The Cook Aug 02 '16 at 11:47
  • see this once it helps you http://stackoverflow.com/questions/6440448/ios-uiwebview-inside-a-uiscrollview – Anbu.Karthik Aug 02 '16 at 11:51

2 Answers2

2

This Question has not been answered yet so i am posting my answer so late .

I had the same problem , i solved with creating custom Delegate. follow the steps below.

1. Create Custom delegate in your UITableViewCell

@protocol MyTableCellDelegate <NSObject>

-(void)webViewDidStartedScrolling;
-(void)webViewDidStoppedScrolling;

@end

2. Add UIScrollViewDelegate in your TableCell.h file

@interface TableCell : UITableViewCell<UIWebViewDelegate,UIScrollViewDelegate>
property(weak,nonatomic) id<MyTableCellDelegate> delegateWebView;

3. in your TableCell.m file

- (void)awakeFromNib {
    [super awakeFromNib];
    self.myWebView.scrollView.delegate=self;
   }

 //ScrollView delegate method 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
      if(self.delegateWebView && [self.delegateWebView respondsToSelector:@selector(webViewDidStoppedScrolling)]){
         [self.delegateWebView webViewDidStoppedScrolling];
     }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{

    if(self.delegateWebView && [self.delegateWebView respondsToSelector:@selector(webViewDidStartedScrolling)]){
        [self.delegateWebView webViewDidStartedScrolling];

    }
 }

4. add MyTableCellDelegate in your UIViewController.h where you are having Your table View.

   @interface UIViewController ()<UITableViewDelegate,UITableViewDataSource,MyTableCellDelegate>

5. Implement the methods of your custom delegate methods in your UIViewController.m file , and enable or disable tableView scrolling , when your webviewDidEndScrolling and webviewDidStartedScrolling

#pragma mark UItableView Delegate

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 YourTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"YourTableCell" forIndexPath:indexPath];
  cell.delegateDealDetails=self;

}
// Custom Delegate Callback method
-(void)webViewDidStartedScrolling
{
    tblDealDetails.scrollEnabled=NO;
}

-(void)webViewDidStoppedScrolling
{

    tblDealDetails.scrollEnabled=YES;
}

This works fine for me , i hope this will help others who is facing same problem.

Dhiru
  • 3,040
  • 3
  • 25
  • 69
0

You can use an disable and enable scroll of the tableView, when the webView scroll delegate is called. UIWebView has a scrollView inside, that you can be I'ts delegate.

- (void)setupSwip
{
    UISwipeGestureRecognizer*   swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwip:)];
    swipeGesture.delegate = self;
    [self.view addGestureRecognizer:swipeGesture];

    self.tableView.scrollEnabled = YES
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    self.tableView.scrollEnabled = NO;
    return YES;
}

- (void)didSwip:(UIGestureRecognizer*)gestureRecognizer
{

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{   
    self.tableView.scrollEnabled = NO;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
    {
        self.tableView.scrollEnabled = YES;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.tableView.scrollEnabled = YES;
}
MCMatan
  • 8,623
  • 6
  • 46
  • 85