2009-09-12 8 views
10

Necesito tener dos UITableViews en una UIView. Puedo hacer que funcione con uno, aquí está el código:Múltiples UITableViews en una UIView

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [contentOne count]; // sets row count to number of items in array 
} 

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    NSString *firstValue = [[NSString alloc] initWithFormat: @"Row %i% %", indexPath.row+1 ]; 
    NSString *secondValue = [contentOne objectAtIndex:indexPath.row]; 

    NSString *cellValue = [firstValue stringByAppendingString: secondValue]; // appends two strings 

    [cell.textLabel setText:cellValue]; 



    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

} 

He intentado varios métodos diferentes. ¿Nadie? Si pudiera nombrar a cada UITableView con un nombre diferente, debería hacerlo, pero no me permitirá editar TableView en ninguna otra cosa sin que se cuelgue.

+0

Duplicar Pregunta Prueba la solución dada en: http://stackoverflow.com/a/11789681/846372 – Soniya

Respuesta

28

por lo que necesita alguna manera de diferenciar entre los dos tableView s Además - se puede establecer la propiedad ya sea "etiquetas" para diferentes valores, o tener una propiedad en su controlador de vista que apunta a cada vista

@property (nonatomic, retain) IBOutlet UITableView *tableView1; 
@property (nonatomic, retain) IBOutlet UITableView *tableView2; 

a continuación, conectar estos a cada vista en constructor de interfaces ...

continuación, en su opinión, los métodos de controlador que puede hacer

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    if (tableView == self.tableView1) { 
     return 37; 
    } else if (tableView == self.tableView2) { 
     return 19; 
    } else { 
     // shouldn't get here, use an assert to check for this if you'd like 
    } 
} 
+0

Probé - (NSInteger) numberOfSectionsInTableView: (UITableView *) tableView { \t if (tableView == self.tableOne) { return 1; \t} \t else if (tableView == self.tableTwo) { \t return 1; \t \t} else { \t \t \t return 1; \t \t}} Me da el error Declaración local de tableView esconde ejemplo –

+0

tiene usted una variable de clase llamado tableView? ¿Está su clase aquí derivada de UITableViewController, o algo más? –

+0

Hice todo dentro de UIView. No tenía un controlador separado para UITableView. Acabo de probar secciones y es mucho más fácil que tratar de hacer dos UITableViews separados. Pude separar mis dos arreglos en secciones separadas. Gracias por ayudar. Simplemente no lo entiendo. –

14

Probablemente la forma más fácil de i Para esto, debe tener dos delegados y clases de origen de datos, uno para cada vista de tabla. Eso reduciría el número de ocurrencias if (tableview == tableview1) en el código del controlador de vista.

+1

¿Podría proporcionarnos una implementación de ejemplo para esto, por favor? – dombesz

+1

pero multiplicaría la cantidad de delegados y métodos de fuentes de datos para implementar. es un escenario de dar y tomar :) –

Cuestiones relacionadas