El texto de la celda de la vista de tabla se superpone a otros cuando se toca varias veces

I created a list of Youtube videos but there are some problems:

- (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 = @"\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;\
    color: white;\
    </head><body style=\"margin:0\">\
    <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
    width=\"%0.0f\" height=\"%0.0f\"></embed>\

    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;

At the beginning, my app works just fine. However, when I click one cell multiple times the label starts overlapping with other cell labels. How can I fix this problem?

preguntado el 08 de noviembre de 11 a las 10:11

Si el problema se resuelve, debe verificar su pregunta como resuelta y elegir una respuesta para ayudar a las personas en el futuro con el mismo problema :) -

2 Respuestas

Intenta configurar cell.clipsToBounds = YES.

respondido 08 nov., 11:14

It's happening because you're creating the titleLabel everytime the function is called. Keep in mind that cell are re-used when table view is scrolled, so basically everytime a cell is used you're re-creating the titleLabel without removing the old one

respondido 08 nov., 11:14

thanks for the reply. but what should I do to fix the problem? - Yangrui

I would move the creation of *titleLabel in the if (cell == nil) block. In this way, you're creating it just when needed. And later you can retrieve it by the tag you're assigning it :) - Hna. Richie

I've tried this but...now the entire order of items become chaotic...and pretty random .. each time I scroll the tableview it return different labels...what's wrong? or should I only move UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelFrame]; into the if block? - Yangrui

If you move NSString *cellValue = [selectedCellItem.title objectAtIndex:indexPath.row]; in the if, it's normal that everything mix up, because you're assigning the content of the cell ONLY when you need to create it. I would only move UILabel *titleLabel = [[UILabel alloc] initWithFrame:labelFrame]; into the if block - Hna. Richie

Seriously, this is going to be mark as an "unresolved" question if you don't mark it as solved :( - Hna. Richie

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.