2013-01-10 41 views
1

我對如何重複使用uitableviewcell有點困惑。如何在UITableViewCell中動態添加UIWebview?

因此,這裏是我的originalcode:

- (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。我怎樣才能實現它?

+0

你必須設置標籤的框架也和你可以替換的UITextView標籤的設置幀也..... –

回答

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; 
} 
+0

是的,我認爲這是我可以執行的最好的事情,以確保性能不受影響。謝謝! – Rendy

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; 
} 
+0

我認爲這會影響tableview的性能,因爲我在tableview中有很多行。不管怎麼說,還是要謝謝你。 – Rendy

+0

從單元中刪除一個元素時,從單元中刪除webView將使單元更輕。這實際上會提高性能。 –