在自定义UITableViewCell上实现UIWebView

时间:2012-11-26 10:44:49

标签: objective-c ios uitableview uiwebview

我在{I}创建的自定义UIWebView中有UITableViewCell

问题是,当我在UIWebView中实现"MyMain"时,它正在运行,但是当我滚动文本时,会一次又一次地在单元格上读取文本。

当我在自定义单元格类中实现UIWebView时,它根本没有显示它。

UITableViewCustomCell.h

@property (nonatomic, weak) IBOutlet UIWebView *wvMainText;
@property (nonatomic, strong) NSString *wvTitle;
@property (nonatomic, strong) NSString *wvPrice;

UITableViewCustomCell.m

@synthesize wvMainText = _wvMainText;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // THIS IS WHERE I IMPLEMENT THE WEBVIEW?
    }
    return self;
}

MyMain

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"myListCell";

    UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UITableViewCustomCell" owner:nil options:nil];

        for (UIView *view in views) {
            if([view isKindOfClass:[UITableViewCell class]])
            {
                cell = (UITableViewCustomCell *)view;
            }
        }
    }

    cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"title"];
    cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"price"];

    wv.scrollView.scrollEnabled = NO;

    NSString *htmlString = [NSString stringWithFormat:
                        @"<html><body>%@,%@</body></html>", self.wvTitle, self.wvPrice];

    [wv loadHTMLString:htmlString baseURL:nil];
    [wv setBackgroundColor:[UIColor clearColor]];
    [wv setOpaque:NO];

    return cell;
}

1 个答案:

答案 0 :(得分:1)

试试这个......它可以解决你的问题。 在你的cellForRowAtIndexPath方法中,请做这样的更改......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath         *)indexPath
{
static NSString *CellIdentifier = @"myListCell";

UITableViewCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"UITableViewCustomCell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (UITableViewCustomCell *)view;
        }
    }
       cell.wvTitle = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"title"];
    cell.wvPrice = [[self.arrList objectAtIndex:indexPath.row] valueForKey:@"price"];

    wv.scrollView.scrollEnabled = NO;

    NSString *htmlString = [NSString stringWithFormat:
                    @"<html><body>%@,%@</body></html>", self.wvTitle, self.wvPrice];

    [wv loadHTMLString:htmlString baseURL:nil];
    [wv setBackgroundColor:[UIColor clearColor]];
    [wv setOpaque:NO];

}


    return cell;
}
相关问题