2010-07-05 19 views
6

Tengo problemas para averiguar cómo mostrar diferentes estilos de celdas, así como también celdas personalizadas en un UITableView. Entiendo cómo configurar y poner celdas juntas y la construcción de un UITableView básico, pero no cómo "mezclar y combinar" la celda en uno.Uso de tipos de estilos de celdas múltiples y celdas personalizadas en una UITableView

El mejor ejemplo que puedo mostrarle sobre lo que estoy tratando de lograr es con la aplicación Tweetie 2. Tweetie 2 profile

En la parte superior del segmento hay un párrafo de bloque, luego debajo de él existe UITableViewCell con el conjunto de estilo UITableViewCellStyleValue2. ¿Cómo exactamente voy a lograr este efecto?

Gracias de antemano

Respuesta

8

El diseño principal es una tabla agrupada. Cada grupo de celdas es una sección de tabla. La celda más alta se establece con un fondo transparente.

La clave para hacer este trabajo es tener una estructura lógica en el delegado de vista de tabla que entienda qué diseño de celda va en qué sección y qué fila. Una declaración de cambio generalmente es más fácil, aunque también puede usar matrices o diccionarios configurados para reflejar el diseño.

Así, en tableView:cellForRowAtIndexPath: que tendría algo como:

switch (indexPath.section) { 
    case 0: 
     cell= //configure cell with transparent background 
     break; 
    case 1: 
     if (indexPath.row==0) { 
      cell = // configure cell for multiline 
     }else { 
      cell = // configure for UITableViewCellStyleValue2 
     } 
     break; 
    case 2: 
     // .. and so on for each section and cell 
     break; 
    default: 
     break; 
} 

En este diseño, la tableview se está utilizando menos como una tabla lógica (que muestra las unidades de datos de la lista estructurada de repetición) y más como mecanismo conveniente para administrar un diseño. La lógica que administra la tabla vista debe ser más compleja y reflejar las necesidades del diseño.

4

El enfoque más directo sería cambiar su implementación de -tableView:cellForRowAtIndexPath: para usar y indexPath.row para determinar qué tipo de celda dibujar. Por ejemplo:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
    if (indexPath.row == 0) { 
     // return long text style cell 

    } else { 
     // return left/right label style cell 
    } 

    } else { 
    // return the 4-way button style cell 
    } 
} 

Dependiendo del número de células se renderiza, y cuántos estilos celular que tienes, puede que tenga que volver a utilizar las células, en cuyo caso se debe utilizar un identificador de celda diferente para cada estilo de celda.

0

Para obtener el mismo efecto que las células de división de Tweetie, crear una célula de medida y agregar un control segmentado y crear el título y las calcomanías detalle alt text

1

Ésta es una manera elegante de hacer:

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

    /* 
     Call a function to create all custom cells. 
     Send the tableview and the indexPath to this function. 
     So, your code will be clean and easy to read an maintenance =D 
     DON'T forget to change the height of each cell 
    */ 
    if (indexPath.row < 3) 
     return [self createACustomCell1:tableView indexPath:indexPath]; 
    else 
     return [self createACustomCell2:tableView indexPath:indexPath]; 

} 


//************* 
// Create CUSTOM CELL 2 
//************* 
-(UITableViewCell *)createACustomCell1:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{ 
    static NSString *CUSTOMCELL_1 = @"CUSTOMCELL_1"; 

    CustomCell_1 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1]; 
    if (!cell){ 
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_1 
              bundle:nil] forCellReuseIdentifier:CUSTOMCELL_1]; 
     cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_1]; 
    } 

    // Cell customization above 
    return cell; 
} 


//************* 
// Create CUSTOM CELL 2 
//************* 
-(UITableViewCell *)createACustomCell2:(UITableView *)anTableView indexPath:(NSIndexPath *)indexPath{ 
    static NSString *CUSTOMCELL_2 = @"CUSTOMCELL_2"; 

    CustomCell_2 *cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2]; 
    if (!cell){ 
    [anTableView registerNib:[UINib nibWithNibName:CUSTOMCELL_2 
              bundle:nil] forCellReuseIdentifier:CUSTOMCELL_2]; 
     cell = [anTableView dequeueReusableCellWithIdentifier:CUSTOMCELL_2]; 
    } 

    // Cell customization above 


    return cell; 
} 
Cuestiones relacionadas