cell加載html文本

.h文件

    //
    //  XBWCSGoodsDetailContentInfoCell.h
    //  wcs
    //
    //  Created by 劉飛 on 2018/8/2.
    //  Copyright © 2018年 ahxb. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    typedef void (^ReloadBlock)(void);
    @interface XBWCSGoodsDetailContentInfoCell : UITableViewCell

    @property(nonatomic,copy)NSString *htmlString;
    @property(nonatomic,copy)ReloadBlock reloadBlock;
    +(CGFloat)cellHeight;

    @end

.m文件

//
//  XBWCSGoodsDetailContentInfoCell.m
//  wcs
//
//  Created by 劉飛 on 2018/8/2.
//  Copyright © 2018年 ahxb. All rights reserved.
//  商品詳情詳情內容區域cell

#import "XBWCSGoodsDetailContentInfoCell.h"
#import <WebKit/WebKit.h>

@interface XBWCSGoodsDetailContentInfoCell()<UIWebViewDelegate,UIWebViewDelegate>//WKUIDelegate,WKNavigationDelegate>

@property (nonatomic,strong)UIWebView *webview;
@end

static CGFloat staticheight = 0;
@implementation XBWCSGoodsDetailContentInfoCell


+(CGFloat)cellHeight
{
    return staticheight;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
    [self.contentView addSubview:self.webview];
}
return self;
}


 -(void)setHtmlString:(NSString *)htmlString
{
_htmlString = htmlString;
self.webview.delegate = self;
if (isEmptyStr(_htmlString)) {
    staticheight = 0;
    if (_reloadBlock) {
        _reloadBlock();
    }
}else{
    [self.webview loadHTMLString:[self reSizeImageWithHTML:_htmlString] baseURL:nil];
}

}
 -(void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
self.webview.frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
self.webview.hidden = NO;
if (staticheight != height+1) {
    staticheight = height+1;
    if (staticheight > 0) {
        if (_reloadBlock) {
            _reloadBlock();
        }
    }
}
}
-(UIWebView *)webview
{
if (!_webview) {
    _webview =[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];//這裏一定要寫1,不然高度不準確
    _webview.userInteractionEnabled = NO;
    _webview.hidden = YES;
}
return _webview;
}



/*
 *  讓html文本適應屏幕
 */
-(nullable NSString *)reSizeImageWithHTML:(NSString *)html {
return [NSString stringWithFormat:@"<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0'><meta name='apple-mobile-web-app-capable' content='yes'><meta name='apple-mobile-web-app-status-bar-style' content='black'><meta name='format-detection' content='telephone=no'><style type='text/css'>img{width:%fpx}</style>%@", SCREEN_WIDTH-SIZE(15), html];//-SIZE(15)是爲了html文本距離右邊一定的間距,不然太緊貼邊緣了
}



@end

ViewController中設置cell的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return [XBWCSGoodsDetailContentInfoCell cellHeight];
}

ViewController中創建 cell

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(bself);
XBWCSGoodsDetailContentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XBWCSGoodsDetailContentInfoCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.htmlString = isEmptyStr(self.goodsDetailModel.content)?@"":self.goodsDetailModel.content;
    cell.reloadBlock =^(void)
    {
        [bself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    };
    return cell;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章