2011-12-09 11 views

Odpowiedz

17

Krok 1: ustawić rozmiar sekcji nagłówka. Przykład następujący.

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

Krok 2: tworzyć & zamian dostosowane nagłówka sekcji.

- (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; 
} 

Etap 3: liczba powrotu części. (Przykład 10 tutaj)

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

Etap 4: liczby wierszy na przekroju. (Na przykład, 4 rzędy dla każdej sekcji)

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

Etap 5: tworzyć & komórki zwrotny (UITableViewCell dla każdego rzędu)

- (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; 
} 

Krok 6: dodać zdarzenie do obsługi TouchDown na nagłówku sekcji.

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

Możesz użyć metody scrollToRowAtIndexPath: atScrollPosition: animated: method z UITableView. Ustaw znacznik przycisk do sekcji i wywołać w swoim działaniu:

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

Dzięki za odpowiedź co0o0ol, i tak naprawdę działa dla mnie .. :-) –

Powiązane problemy