Blog
>Lista ejercicios >
Ejercicio 7
Hola developer, hoy en este ejercicio vamos a aprender a usar el
UITableContoller, por un lado vamos a ver una lista de elementos y por
otro sus detalles.
Ejercicio 7 : Aprendiendo a hacer una tabla de contenidos mediante UITableView
Creamos un proyecto nuevo : file - new - proyect - Simple View Application. Como todos los tutoriales nos centramos sólo en iPhone.
Para comenzar agregamos un objeto Table View y conectamos el dataSource y el delegate al controlador
Ahora abriremos el ViewController.h y añadimos la conexion de la tabla y las propiedades UITableViewDelegate y UITableViewDataSource, además del array que contendrá los datos.
Asignamos ahora la conexion de la table mediante IBOutlet a la tabla en el StroyBoard
Bien, ahora iremos al código del ViewController.m para inicializar el array en el metodo viewDidLoad, y pasamos a implementar los métodos principales para cumplir con el UITableViewDelegate y mostrar los datos.
Y por último implementamos el método por el cual al clickar en cualquier item realiza una acción
Y este es el resultado :
Ejercicio 7 : Aprendiendo a hacer una tabla de contenidos mediante UITableView
Creamos un proyecto nuevo : file - new - proyect - Simple View Application. Como todos los tutoriales nos centramos sólo en iPhone.
Para comenzar agregamos un objeto Table View y conectamos el dataSource y el delegate al controlador
Ahora abriremos el ViewController.h y añadimos la conexion de la tabla y las propiedades UITableViewDelegate y UITableViewDataSource, además del array que contendrá los datos.
@interface ViewController : UIViewController { IBOutlet UITableView *tabla; NSMutableArray *items; } @end
Asignamos ahora la conexion de la table mediante IBOutlet a la tabla en el StroyBoard
Bien, ahora iremos al código del ViewController.m para inicializar el array en el metodo viewDidLoad, y pasamos a implementar los métodos principales para cumplir con el UITableViewDelegate y mostrar los datos.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
items = [NSMutableArray arrayWithObjects:@"Item 0",@"Item 1",@"Item 2",@"Item 3",@"Item 4",@"Item 5",@"Item 6",@"Item 7",@"Item 8",@"Item 9",@"Item 10",@"Item 11",@"Item 12",@"Item 13",@"Item 14",nil];
}
//METODO NECESARIO PARA DEVOLVER EL NÚMERO DE SECCIONES DE LA TABLA
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//METODO NECESARIO PARA MOSTRAR EL NUMERO DE FILAS DE LA TABLA
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [items count];
}
//METODO QUE INTRODUCE LOS VALORES DEL ARRAY EN LAS CELDAS
-(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//RECUPERAMOS ARRAY SEGUN LA POSICION DE LA FILA
NSString *fila = [items objectAtIndex:indexPath.row];
//CREAMOS LA CELDA
UITableViewCell *celda =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
//COPIAMOS LA VARIABLE DEL ARRAY EN LA FILA
celda.textLabel.text=fila;
//DEVOLVEMOS LA CELDA
return celda;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Y por último implementamos el método por el cual al clickar en cualquier item realiza una acción
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// En este método haremos lo que creamos oportuno. Por lo general pasaremos a una vista detalle del elemento seleccionado. Haciendo uso de UINavigationController que se puede ver en el correspondiente tutorial.
NSString *dato = [datos objectAtIndex:indexPath.row];
UIAlertView *alerta = [[UIAlertView alloc]
initWithTitle:[NSString stringWithFormat:@"Has pulsado la fila %d",indexPath.row]
message:[NSString stringWithFormat:@"¡No te olvides de visitar ThebestAndroide!"]
delegate:nil
cancelButtonTitle:@"Aceptar"
otherButtonTitles:nil, nil];
[alerta show];
}
Y este es el resultado :
|
Indice
|
Ejercicio anterior |
Siguiente ejercicio |


