恥知らずのウェブエンジニア -web engineer, shameless

これは一歩を踏み出すことができない者たちのブログ

ios UITableViewCellにWebViewを表示

表題の件、メモ。

cellForRowAtIndexPathメソッド内でwebViewを生成して、
[cell.contentView addSubview:webView];で追加する。
ラベルやテキストも同じ要領で追加が可能。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hogeCell"];
    //セルの大きさ取得    
    CGRect frame = cell.contentView.bounds;
    frame = CGRectInset(frame, 5, 5);
 //webView生成
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    NSString *HTML = @"<html>hoge</html>";
    [webView loadHTMLString:HTML baseURL:nil];

    webView.delegate = self;
    [cell.contentView addSubview:webView];
    
    return cell;
}




感謝致します。

f:id:ogataka50:20140412143333j:plain