Pregunta

en mi UITableView quiero configurar para la primera noticia de un feed RSS un tableViewCell personalizado (el Tipo A digamos) y para las otras noticias segundo, tercero, etc. otro tableViewCell personalizado (trype B) el problema es que el el tableViewCell personalizado (trype A) creado para la primera noticia se reutiliza, pero curiosamente el número de filas entre el primer uso del customViewCell (tipo A) y la segunda aparición del mismo tipo de customViewCell no es igual ...

mi cellForRowAtIndexPath se parece a esto.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
    static NSString *CellIdentifier = @"Cell";

    if(feedIndex == 0){
        MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    else{
        NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    return nil;
}    

los dos tipos de celdas tienen alturas diferentes que se configuran correctamente. ¿Alguien podría indicarme la dirección correcta sobre cómo hacer que la celda personalizada tipo A aparezca solo para las primeras noticias (no se reutiliza)? gracias

¿Fue útil?

Solución

Debería crear un identificador de celda diferente para los dos estilos de celda:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];

static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";

if(feedIndex == 0) {

   MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

   if (cell == nil) {
       cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}
else {
   NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

   if (cell == nil) {
       cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}

return nil;
}

Otros consejos

No entiendo completamente tu pregunta, pero noté dos cosas curiosas. Si está utilizando dos tipos de células diferentes, debe usar dos identificadores de células distintos cuando llame a 'dequeueReusableCellWithIdentifier'. Actualmente estás usando el mismo identificador para ambos, lo cual es incorrecto. Intenta algo como:

static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";

Además, nunca he visto nada como:

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];

¿Por qué no solo usar:

int feedIndex = indexPath.row;

en cellForRowAtIndexPath

if ("Condition for cell 1") {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    } else {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    }

[self configureCell:cellV indexpath:indexPath withClipVo:clip];

return cellV;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top