2011-08-15 15 views
5

Soy nuevo en el mundo de iOS y quiero saber cómo hacer un UITableView con celdas personalizadas que se ven y se comportan como la que tiene cuando intenta configurar alguna conexión WiFi en su dispositivo. (Conoces el UITableView con celdas que contienen UITextField s con fuente azul donde configuras la dirección IP y todo eso ...).UITableView editable con un campo de texto en cada celda

Respuesta

9

Para hacer una distribución de celdas personalizada, se requiere un poco de codificación, así que espero que no lo asuste.

Lo primero es crear una nueva subclase UITableViewCell. Llamémoslo InLineEditTableViewCell. Su interfaz de InLineEditTableViewCell.h podría ser algo como esto:

#import <UIKit/UIKit.h> 

@interface InLineEditTableViewCell : UITableViewCell 

@property (nonatomic, retain) UILabel *titleLabel; 
@property (nonatomic, retain) UITextField *propertyTextField; 

@end 

Y su InLineEditTableViewCell.m podría tener este aspecto:

#import "InLineEditTableViewCell.h" 

@implementation InLineEditTableViewCell 

@synthesize titleLabel=_titleLabel; 
@synthesize propertyTextField=_propertyTextField; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Here you layout your self.titleLabel and self.propertyTextField as you want them, like they are in the WiFi settings. 
    } 
    return self; 
} 

- (void)dealloc 
{ 
    [_titleLabel release], _titleLabel = nil; 
    [_propertyTextField release], _propertyTextField = nil; 
    [super dealloc]; 
} 

@end 

Lo siguiente es que configurar su UITableView como lo haría normalmente en su controlador de vista. Al hacer esto, debe implementar el método de protocolo UITablesViewDataSource- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath. Antes de insertar su implementación para esto, recuerde el #import "InLineEditTableViewCell" en su controlador de vista. Después de hacer esto, la implementación es la siguiente:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    InLineEditTableViewCell *cell = (InLineEditTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"your-static-cell-identifier"]; 

    if (!cell) { 
     cell = [[[InLineEditTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"your-static-cell-identifier"] autorelease]; 
    } 

    // Setup your custom cell as your wish 
    cell.titleLabel.text = @"Your title text"; 
} 

Eso es todo! Ahora tiene celdas personalizadas en su UITableView.

¡Buena suerte!

+3

Gracias por su ayuda. En realidad, no tengo miedo de codificar, no soy bueno para manipular elementos gráficos :) – Zak001

Cuestiones relacionadas