iOS tableViewCell展示 UIWebView加载所有内容后禁止滚动

是这样 cell 展示一个网页所有内容然后撑开cell刷新cell高度, 然后禁掉滚动事件。这样完美实现一个自适应高度的html/富文本样式。

    // 赋值部分 NSUserDefaults 高度保存 写在cell里面

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];


    if ([user objectForKey:webHight]) {

        self.webviewHight.constant = [NSString stringWithFormat:@"%@", [user objectForKey:webHight]].floatValue;


        self.webView.delegate = nil;

        [user removeObjectForKey:webHight];

        [user synchronize];


    }else{

        self.webView.delegate  = self;

    }

    

    UIScrollView *first_tempView  = (UIScrollView *)[self.webView.subviews objectAtIndex:0];

    

    first_tempView.scrollEnabled = NO;


#pragma mark ----------------------- webView 代理 加载完后获取高度 ------------------------

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    // 获取webView的高度

    CGFloat webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];


    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];


    [user setObject:[NSString stringWithFormat:@"%f",webViewHeight] forKey:webHight];


    [user synchronize];

    

    // 发通知告诉tableview 刷新 tableView接收通知然后reloaddata就可以了

    [self sendMessage];

 

}


#pragma mark ----------------------- 获取高度去刷新 ------------------------

-(void)sendMessage

{

    //创建通知

    NSNotification *notification =[NSNotification notificationWithName:rechNotifyName object:nil userInfo:nil];

    //通过通知中心发送通知

    [[NSNotificationCenter defaultCenter] postNotification:notification];

}



#pragma mark ----------------------- 更新高度通知 tableView 里面 ------------------------

-(void)getnote{

    //注册通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:rechNotifyName object:nil];

}


- (void)tongzhi:(NSNotification *)text{

    

    [_tableView reloadData];


}

// 移除监听的事件

-(void)viewWillDisappear:(BOOL)animated

{

    [super viewWillDisappear:animated];

    [[NSNotificationCenter defaultCenter] removeObserver:self name:rechNotifyName  object:nil];

}







猜你喜欢

转载自blog.csdn.net/saw471/article/details/80513240