2009-09-10 7 views
52

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

my cellForRowAtIndexPath se ve así.

- (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 están configuradas correctamente. ¿podría alguien señalarme en la dirección correcta sobre cómo hacer que la celda personalizada tipo A aparezca solo para las primeras noticias (que no se vuelva a utilizar)? gracias

+0

para el La versión rápida de esta pregunta se ve [aquí] (http://stackoverflow.com/ preguntas/30774671/uitableview-with-more-one-custom-cells-with-swift) – Honey

Respuesta

78

Debe crear un identificador de celda diferente para los dos tipos de células:

- (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; 
} 
+0

gracias, funcionó. ¿Sabes si es posible establecer diferentes células personalizadas para diferentes secciones de un tableView? la sección 0 tendrá el tipo A y la sección 1 tendrá el tipo B. Gracias –

+0

encontré el answear .. (NSIndexPath *) indexPath tiene la propiedad de la fila también la propiedad de la sección que fue útil :) Sorin –

+0

int feedIndex = indexPath.row; - es lo suficientemente bueno en la primera línea –

8

no entiendo del todo su pregunta, pero notado dos cosas curiosas. Si usa dos tipos de celda diferentes, necesita usar dos identificadores de celda distintos cuando llame a 'dequeueReusableCellWithIdentifier'. Actualmente estás usando el mismo identificador para ambos, que es incorrecto. Pruebe algo como:

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

Además, nunca he visto algo como:

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

Por qué no sólo tiene que utilizar:

int feedIndex = indexPath.row; 
0

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; 
Cuestiones relacionadas