switch stepper iphone cocoa-touch

iphone - stepper - Vistas de encabezado TableView reutilizables



switch ios (3)

La razón por la que Apple incorporó la capacidad de reutilizar las celdas de vista de tabla es que, si bien la vista de tabla puede tener muchas filas, solo se muestran unas pocas en la pantalla. En lugar de asignar memoria para cada celda, las aplicaciones pueden reutilizar las celdas ya existentes y reconfigurarlas según sea necesario.

En primer lugar, las vistas de encabezado son solo UIViews, y si bien UITableViewCell es una subclase de UIView, no se pretende colocarlas como la vista de un encabezado de sección.

Además, como generalmente tendrá muchos menos encabezados de sección que filas totales, hay pocas razones para crear un mecanismo de reutilización y, de hecho, Apple no ha implementado uno para UIViews genéricos.

Tenga en cuenta que si solo está estableciendo una etiqueta en el encabezado, puede usar -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section lugar.

Para algo más personalizado, como una etiqueta con texto rojo (o un botón, imagen, etc.), puedes hacer algo como esto:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease]; label.textColor = [UIColor redColor]; label.text = [NSString stringWithFormat:@"Section %i", section]; [headerView addSubview:label]; return headerView; }

Por motivos de rendimiento, es habitual reutilizar las celdas de UITableView. ¿Hay alguna manera de hacer lo mismo con las vistas del encabezado de TableView? Estoy hablando de los que se devuelven con el método del delegado:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

Intenté hacer lo siguiente que no parece funcionar como se esperaba:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { static NSString *CellIdentifier = @"Header"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; if (cell == nil) { cell = [self getHeaderContentView: CellIdentifier]; } return cell; }

¿Hay alguna forma de reutilizar las vistas del encabezado?


Puede implementar eso creando la clase UITableViewHeaderFooterView que es subclase de UIView . También necesita crear un XIB individual ya que no se creará automáticamente con la creación de UITableViewHeaderFooterView .

Registrar Nib con tableview

[self.tblCart registerNib:[UINib nibWithNibName:@"CartHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CartHeader"];

Ahora puedes acceder a eso en viewForHeaderInSection

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { CartHeaderView *sectionHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CartHeader"]; return sectionHeader; }

Nota: para establecer el color de fondo, deberá crear una subvista con el mismo marco que el encabezado de la sección y establecer el color para esa vista.

puedes seguir

Cambiar el color de fondo en un UITableViewHeaderFooterView cargado desde un xib dice que use contentView.backgroundColor en su lugar


Una solución simple pero efectiva:

@interface SectionedTableViewController () @property (nonatomic, strong) UINib* sectionHeaderNib; @property (nonatomic, strong) NSMutableArray* sectionHeaders; @end @implementation SectionedTableViewController @synthesize sectionHeaderNib = sectionHeaderNib_; @synthesize sectionHeaders = sectionHeaders_; - (void) viewDidUnload { self.sectionHeaders = nil; [super viewDidUnload]; } - (NSMutableArray*) sectionHeaders { if (!sectionHeaders_) self.sectionHeaders = [NSMutableArray array]; return sectionHeaders_; } - (UINib*) sectionHeaderNib { if (!sectionHeaderNib_) self.sectionHeaderNib = [UINib nibWithNibName: NSStringFromClass(YourHeaderView.class) bundle: nil]; return sectionHeaderNib_; } - (YourHeaderView*) dequeueHeader { return [self.sectionHeaders firstObjectPassingTest: ^(id obj) { return (BOOL) ([obj superview] == nil); }]; } - (NSString*) sectionHeaderTitleForSection: (NSInteger) section { return nil; } - (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section { YourHeaderView* headerView = [self dequeueHeader]; if (!headerView) { headerView = [YourHeaderView instanceFromNib: self.sectionHeaderNib]; [self.sectionHeaders addObject: headerView]; } return headerView; } @end