多次点击时,表格视图单元格文本与其他文本重叠

时间:2011-11-08 10:15:48

标签: objective-c ios cocoa-touch uitableview uiwebview

我创建了Youtube视频列表,但存在一些问题:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *url = [selectedCellItem.content objectAtIndex:indexPath.row];

    UIWebView *wbView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, ROW_HEIGHT, ROW_HEIGHT)];
    wbView.tag = 1;
    [cell.contentView addSubview:wbView];
    [wbView release];

    NSString *embedHTML = @"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    }\
    </style>\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\
    </body></html>";

    NSString *html = [NSString stringWithFormat:embedHTML,url, ROW_HEIGHT, ROW_HEIGHT];
    UIWebView *thisVideoView = (UIWebView *)[cell.contentView viewWithTag:1];  
    thisVideoView.delegate = self;
    [thisVideoView loadHTMLString:html baseURL:nil];

    CGFloat cellWidth = [UIScreen mainScreen].bounds.size.width-ROW_HEIGHT-5;
    NSString *cellValue = [selectedCellItem.title objectAtIndex:indexPath.row];
    CGRect labelFrame = CGRectMake(ROW_HEIGHT+5, 0.0, cellWidth, ROW_HEIGHT);
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelFrame];

    titleLabel.tag = 2;
    titleLabel.text = cellValue;
    titleLabel.font = [UIFont systemFontOfSize:14.0];
    [titleLabel setNumberOfLines:10];
    [cell.contentView addSubview:titleLabel];
    [titleLabel release];

    return cell;
}

一开始,我的应用程序运行正常。但是,当我多次单击一个单元格时,标签开始与其他单元格标签重叠。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

尝试设置cell.clipsToBounds = YES

答案 1 :(得分:2)

这种情况正在发生,因为每次调用函数时都会创建titleLabel。请记住,滚动表格视图时会重复使用单元格,因此基本上每次使用单元格时都会重新创建titleLabel而不删除旧单元格

相关问题