2011-06-29 9 views
7

tengo estos métodos delegado para un tableview dentro de una clase:IOS: métodos de delegado Tableview para dos tableview

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [array1 count]; 
} 

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


static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
} 

cell.textLabel.text = [array1 objectAtIndex:indexPath.row]; 

return cell; 
} 

si tengo una sola UITableView que está bien, pero si tengo dos UITableView? ¿Cómo puedo organizar mi código? con la etiqueta?

Respuesta

13

Vea cómo todos los métodos de delegado tienen un tableView:(UITableView *)tableView en ellos?

Puede definir sus vistas de tabla en el archivo de cabecera y luego simplemente ir: (asumiendo que su tabla se denomina myTable)

if (tableView == myTable) 

, entonces puede tener tantas vistas de tabla como desee.

Así, por ejemplo:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [array1 count]; 
} 

se convierte en:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == myTable) 
    { 
     return [array1 count]; 
    } 
    if (tableView == myTable2) 
    { 
     return [array2 count]; 
    } 

    return 0; 
} 
0

Cada uno de esos métodos pasa una referencia a la vista de tabla que está llamando. Normalmente conecto cada tabla a una salida en el constructor de interfaz y devuelvo el origen de datos condicionalmente en base a una comparación con tableView en los métodos de delegado y los nombres de salida. Hacerlo con una etiqueta también es posible, pero más complicado y más abierto a las complicaciones al editar la estructura de vista.

3

Mi sugerencia es que su origen de datos actúe como delegado de la vista de tabla, en lugar de su controlador.

Este es un diseño más cercano al patrón Model-View-Controller y le permitirá mucha más flexibilidad y evitará comprobar el valor específico que el argumento tableView tiene en sus métodos de delegado.

En su caso, su delegado/fuente de datos sería una clase con un miembro del tipo NSArray y también implementando el protocolo UITableViewDelegate.

1

Sí, puedes hacerlo con la etiqueta. Dar a sus UITableViews las etiquetas 1 y 2.

estableció un interruptor:

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

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if(cell == nil){ 
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewStylePlain reuseIdentifier:CellIdentifier] autorelease] ; 
} 

switch ([tableView tag]) { 
    case 1:{ 
    [[cell textLabel] setText:@"First tag"] 
    break; 
    } 
    case 2:{ 
    [[cell textLabel] setText:@"Second tag"] 
    break; 
    } 
    default: 
    break; 
} 

return cell; 
} 
+0

el uso de las etiquetas de Niza, mucho más fácil de manejar múltiples tablas – Vincent

Cuestiones relacionadas