如何在UITableViewCell中动态添加UIWebview?

时间:2013-01-10 04:00:12

标签: ios objective-c uitableview

我对uitableviewcell如何重复使用感到困惑。

所以这是我的原始代码:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"ProductsViewCell";
  UITableViewCell *cell = 
      [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell =
        [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIWebView* webView = [[UISynchedWebView alloc] initWithFrame:
                          CGRectMake(0,0, 320, 44)];
    webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    webView.tag = 1001;
    webView.userInteractionEnabled = NO;
    webView.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];

    [cell addSubview:webView];
  }

  UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
  UIFont *font = [UIFont fontWithName:@"Arial" size:15.0f];

  NSString *html =
  [NSString stringWithFormat:
   @"<html>\n"
   "<head>\n"
   "</head>\n"
   "<body><div style=\"padding:5px 0px; text-align:center\"><b>test</b></div></body>\n"
   "</html>"];

  [webView loadHTMLString:html
                  baseURL:nil];

  return cell;
}

我有一个if条件如下:

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"ProductsViewCell";
  UITableViewCell *cell = 
      [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell =
        [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    UIWebView* webView = [[UISynchedWebView alloc] initWithFrame:
                          CGRectMake(0,0, 320, 44)];
    webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    webView.tag = 1001;
    webView.userInteractionEnabled = NO;
    webView.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];

    [cell addSubview:webView];
  }

  //some process here to get isHTML...

  if (isHTML) {
    UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
    UIFont *font = [UIFont fontWithName:@"Arial" size:15.0f];

    NSString *html =
    [NSString stringWithFormat:
    @"<html>\n"
    "<head>\n"
    "</head>\n"
    "<body><div style=\"padding:5px 0px; text-align:center\"><b>test</b></div></body>\n"
    "</html>"];

    [webView loadHTMLString:html
                    baseURL:nil];
  }
  else{
    cell.textLabel.text = @"Not HTML";
  }
  return cell;
}

由于我的内容是动态的,因此我想仅在内容为webview的情况下加载html,否则我想将其作为cell UILabel中的普通文本加载并使用上面的内容条件,我将获得webview与单元格UILabel的重叠。我怎样才能实现它?

2 个答案:

答案 0 :(得分:1)

试试这个

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  static NSString *CellIdentifier = @"ProductsViewCell";
  UITableViewCell *cell = 
      [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell =
        [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
        reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  }

  //some process here to get isHTML...

  if (isHTML) {
    UIWebView* webView = [[UISynchedWebView alloc] initWithFrame:
                          CGRectMake(0,0, 320, 44)];
    webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    webView.tag = 1001;
    webView.userInteractionEnabled = NO;
    webView.opaque = NO;
    webView.backgroundColor = [UIColor clearColor];
    [cell addSubview:webView];

    NSString *html =
    [NSString stringWithFormat:
    @"<html>\n"
    "<head>\n"
    "</head>\n"
    "<body><div style=\"padding:5px 0px; text-align:center\"><b>test</b></div></body>\n"
    "</html>"];

    [webView loadHTMLString:html
                    baseURL:nil];
  }
  else{
      UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
      if(webView)
      {
           [webView removeFromSuperview];
      }
      cell.textLabel.text = @"Not HTML";
  }
  return cell;
}

答案 1 :(得分:0)

UIWebView* webView = (UIWebView*)[cell viewWithTag:1001];
if (isHTML) {
    UIFont *font = [UIFont fontWithName:@"Arial" size:15.0f];
    NSString *html =
    [NSString stringWithFormat:
    @"<html>\n"
    "<head>\n"
    "</head>\n"
    "<body><div style=\"padding:5px 0px; text-align:center\"><b>test</b></div></body>\n"
    "</html>"];

    [webView loadHTMLString:html
                    baseURL:nil];
    webView.hidden = NO;
} else {
    cell.textLabel.text = @"Not HTML";
    webView.hidden = YES;
}