2011-12-09 11 views

Respuesta

17

Paso 1: establecer el tamaño de la Cabecera de la sección. Ejemplo de la siguiente manera.

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

Paso 2: crear & retorno el encabezado de sección personalizada.

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *aView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 55)]; 
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [btn setFrame:CGRectMake(0, 0, 320, 55)]; 
    [btn setTag:section+1]; 
    [aView addSubview:btn]; 
    [btn addTarget:self action:@selector(sectionTapped:) forControlEvents:UIControlEventTouchDown]; 
    return aView; 
} 

Paso 3: número de devolución de secciones. (Por ejemplo 10 aquí)

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

Paso 4: número de filas por sección. (Por ejemplo, 4 filas para cada sección)

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 4; 
} 

Paso 5: crear & célula de retorno (UITableViewCell para cada fila)

- (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.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 
    cell.textLabel.text=[NSString stringWithFormat:@"%i_%i",indexPath.section,indexPath.row]; 

    return cell; 
} 

Paso 6: añadir el evento a manejar la TouchDown en el encabezado de sección.

- (void)sectionTapped:(UIButton*)btn { 
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
} 
2

Puede usar el método scrollToRowAtIndexPath: atScrollPosition: animado: de UITableView. Establecer la etiqueta de botón a la sección y llaman en su acción:

[tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:button.tag] 
atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
+0

Gracias por la respuesta co0o0ol, y realmente realmente funciona para mí .. :-) –

Cuestiones relacionadas