UITableView で複数の UIWebView を使用することはお勧めできません。しかし、短時間でアプリを構築したい場合があるため、UIWebView は非常に便利なクラスです。
私の場合、カスタム UITableViewCell を設定しました
CustomTableViewCell.h
#import <UIKit/UIKit.h>
@interface VerticalTableViewCell : UITableViewCell
@property (nonatomic,retain) UIWebView * webView;
-(void)setWebViewContent:(NSString *)htmlContent andIndex:(NSString *)row;
@end
CustomTableViewCell.m
#import "CustomTableViewCell.h"
@interface CustomTableViewCell()
@end
@implementation VerticalTableViewCell
@synthesize webView;
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
if (self) {
self.webView = [[UIWebView alloc]init];
[self.webView setFrame:self.contentView.frame];
[self.webView setUserInteractionEnabled:NO];
[self.contentView addSubview:self.webView];
}
return self;
}
-(void)setWebViewContent:(NSString *)htmlContent andIndex:(NSString *)row
{
[self.webView loadHTMLString:[NSString stringWithFormat:@"<html><head></head><body><div style='width:414px;height:414px;background-image:url(%@);background-size:414px 414px;'></div><p>现在是第%@排</p></body></html>",htmlContent,row] baseURL:nil];
}
- (void) layoutSubviews
{
[super layoutSubviews];
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.width = [[UIScreen mainScreen] bounds].size.width;
contentViewFrame.size.height = 586.0f;
self.contentView.frame = contentViewFrame;
self.webView.frame = contentViewFrame;
}
-(void)dealloc
{
[self.webView loadHTMLString:nil baseURL:nil];
self.webView = nil;
}
@end
CustomTableView.m
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CMainCell = @"CMainCell";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
if (cell == nil) {
cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CMainCell];
//自定义cell的内容
}
if (self.whiteData!=nil) {
NSString * row = [NSString stringWithFormat:@"%ld",indexPath.row];
[cell setWebViewContent:self.whiteData[indexPath.row] andIndex:row];
//[cell setCustomImage:self.whiteData[indexPath.row]];
}
return cell;
}
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomTableViewCell * customCell = (CustomTableViewCell *)cell;
[customCell.webView loadHTMLString:nil baseURL:nil];
}
...
メソッドを使用したことがわかります:
-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
このメソッドでは、セルが tableView で非表示の場合、cells は webViews を空に設定します。
私たちの場合、UITableView の再利用機能のため、セルは contentView をクリーンアップしません。したがって、ユーザーは tableView を非常に高速にスクロールし、セルは再利用され、HTML コンテンツはまだそこにあります。contentView をクリーンアップすると、うまくいきます。
しかし、別の問題はまだここにあります。セルは、可視領域に入ったときに HTML コンテンツをレンダリングする必要があるためです。UIWebView のレンダリング速度はそれほど速くないため、レンダリング プロセスを最適化するためのコードを記述する必要があります。
これがあなたに役立つことを願っています.乾杯.