2012-04-03 16 views
13

Soy nuevo en el desarrollo de aplicaciones de iOS y Objective C en sí, así que probablemente tenga una pregunta muy simple.Crear un TableVIew mediante programación con Objective-C iOS

Actualmente tengo el siguiente método que se llama desde un botón de la barra de herramientas haga clic en. El método está diseñado para crear una vista de tabla en la variable de marco fr.

- (IBAction)addGolfer:(id)sender { 
    CGRect fr = CGRectMake(101, 45, 100, 416); 

    UITableView *tabrleView = [[UITableView alloc] 
     initWithFrame:fr 
     style:UITableViewStylePlain]; 

    tabrleView.autoresizingMask = 
     UIViewAutoresizingFlexibleHeight | 
     UIViewAutoresizingFlexibleWidth; 
    tabrleView.delegate = self; 
    tabrleView.dataSource = self; 
    [tabrleView reloadData]; 

    self.view = tableView; 
} 

El resultado de llamar a este método no es lo que esperaba. En lugar de crear la vista de tabla en el marco "fr", la vista de tabla llena toda la pantalla.

Otra vez soy totalmente nuevo y agradecería cualquier respuesta y cualquier sugerencia. ¡Gracias!

Respuesta

18

Al configurar dataSourcedelegate y propiedades de su UITableView, quiere decir, usted tiene que escribir métodos al menos para esta dataSource:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 

Si no desea hacer esto, será accidente.Resumen obtendrá este (este código puede contener errores de sintaxis o lógica - Lo escribí en el bloc de notas):

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> { 
    UITableView *firstTableView; 
    UITableView *secondTableView; 
} 

@end 

//

@implementation YourViewController 

#pragma mark - Objects Processing 

- (void)addGolfer:(UIBarButtonItem *)sender { 
    if (secondTableView) { 
     [secondTableView removeFromSuperView]; 
     secondTableView = nil; 
    } 

    secondTableView = [[UITableView alloc] initWithFrame:CGRectMake(101, 45, 100, 416)]; 
    secondTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    secondTableView.delegate = self; 
    tabrleView.dataSource = self; 

    [self.view addSubview:secondTableView]; 
} 

#pragma mark - TableView DataSource Implementation 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (tableView == firstTableView) { // your tableView you had before 
     return 20; // or other number, that you want 
    } 
    else if (tableView == secondTableView) { 
     return 15; // or other number, that you want 
    } 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *cellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 

    cell.backgroundView = [[UIView alloc] init]; 
    [cell.backgroundView setBackgroundColor:[UIColor clearColor]]; 
    [[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)]; 

    if (tableView == firstTableView) { // your tableView you had before 
     // ... 
    } 
    else if (tableView == secondTableView) { 
     cell.titleLabel.text = [NSString stringWithFormat:@"Cell %d", indexPath.row + 1]; 
    } 

    return cell; 
} 

@end 
4

En lugar de establecer la vista de UIViewController, agregue tableView como subvista.

En lugar de:

self.view = tableView; 

hacer esto:

[self.view addSubview:tableView]; 

Esto respetar adecuadamente el marco que se establece.

+0

que lo hice, pero ahora no pasa nada. --- Lo siento, ahora recibo un error. –

+0

Podría ser que haya mal escrito 'tabrleView'. Debería ser 'tableView' en todas partes. – DHamrick

+0

Sí, lo hice porque tenía otra TableView –

16

Paso 1: Añadir delegado UITableViewDataSource,UITableViewDelegate

@interface viewController: UIViewController<UITableViewDataSource,UITableViewDelegate> 
{ 
    UITableView *tableView; 
} 

Paso 2:

-(void)viewDidLoad 
{ 
    tableView=[[UITableView alloc]init]; 
    tableView.frame = CGRectMake(10,30,320,400); 
    tableView.dataSource=self; 
    tableView.delegate=self; 
    tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
    [tableView reloadData]; 
    [self.view addSubview:tableView]; 
} 

Paso 3: Propiedades en tableview (filas & columna)

// - Por ninguna de filas de la tabla

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

// - altura Encabezado de tabla si es necesario

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 50; 
} 

// - Asignar datos a las celdas

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath] ; 

    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    cell.textLabel.text=[your_array objectAtIndex:indexPath.row]; ***(or)*** cell.textLabel.text = @"Hello"; 
    return cell; 
} 

// - Operación cuando las células táctiles

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Your custom operation 
} 
Cuestiones relacionadas