【问题标题】:UIWebView in UITableView loses textUITableView 中的 UIWebView 丢失文本
【发布时间】:2011-08-19 14:17:34
【问题描述】:

我只是将我的 tableview 更改为读取富文本而不是纯文本,所以我的解决方案是将富文本加载到 UIWebview 中,然后将 webview 添加到 tableview 单元格内容视图中。 但是,通过此更改,文本将不再显示。这是 -cellForRowAtIndexPath 的代码:

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

    int row = indexPath.row;

    NSString *cellString = [theArray objectAtIndex:row];

    // replaced    cell.textLabel.text=cellString;       
    // with

    UIWebView *webTVCell = [[UIWebView alloc] init];
    [webTVCell loadHTMLString:cellString baseURL:nil];
    [cell.contentView addSubview:webTVCell];
    [webTVCell release];

    return cell;

但是,cellString 中的文本不再出现在 tableview 的任何单元格中。有人可以帮忙吗?将不胜感激。

【问题讨论】:

    标签: uitableview uiwebview


    【解决方案1】:
    UIWebView *webTVCell = [[UIWebView alloc] initWithFrame:cell.bounds];
    

    试试这个而不是UIWebView *webTVCell = [[UIWebView alloc] init];

    UIWebView 不适合在另一个滚动条中工作(如 UITableView);

    要调整单元格大小,您应该使用UITableView.rowHeight 属性或以下UITableViewDelegate 方法:

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

    顺便说一句,[UITableView reloadData] 通常是更新表的错误决定。

    【讨论】:

    • 成功了!谢谢!但是现在还有一个问题:我在 -heightForRowAtIndexPath 中调整了每个单元格的大小,因此 tableviewcells 具有适当的高度。但是 webview 只显示了大约一半的内容;我可以向下滚动到文本的其余部分,但它隐藏在单元格的底部。有没有办法让 webview 填充每个单元格的内容,而不是滚动?感谢您的帮助。
    • [webTVCell setScalesPageToFit:YES];
    • 将文本的大小设置得如此微小以至于无法阅读;另外,我设置了 tableviewcells 的背景颜色,但文本区域是灰色的,单元格的其余部分是我指定的颜色。
    • 嗨 Madhu,我最初有 UIWebView *webTVCell = [[UIWebView alloc] init];但文本根本没有出现;在我使用 webview 之前,我通过使用 -heightForRowAtIndexPath; 获得了单元格的正确高度;当我追求 webview 解决方案时,这一切都消失了。另外,不明白您对 [UITableView reloadData] 的评论;我没有使用那种方法。我要做的就是使用 webview,这样我就可以在 tableviewcells 中呈现富文本;这就是我尝试使用 uiwebview 的原因。
    • 使用 [[UIWebView alloc] initWithFrame:cell.bounds],只有部分 webview 显示在单元格中,就像它在单元格的其余部分后面一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 2011-04-04
    • 2012-06-27
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多