2011-12-09 10 views
7

を最上部に移動UITableViewで、そのセクションの見出しをそこにUIButtonを置いCustom Section Headerその非常に私はUIButtonをクリックすること、である必要right.What特にSection HeaderはTopにスクロールする必要があります。それはそれです我々はのUITableViewでカスタムセクションのヘッダーをクリックすると、その部分は私が持っている

任意の提案、コードの一部が賞賛されます。

答えて

17

ステップ1:は、セクションヘッダーのサイズを設定します。例を次に示します。

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

ステップ2:カスタマイズされたセクションヘッダを返す&作成します。

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

ステップ3:セクションの戻り番号。 (ここでは例えば10)

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

ステップ4:セクションごとの行の数。 (例えば、セクションごとに4行)

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

ステップ5:を処理するためにイベントを追加:(Row毎のUITableViewCell)&戻りセル

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

ステップ6を作成しますセクションヘッダーのTouchDown

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

UITableViewのscrollToRowAtIndexPath:atScrollPosition:animated:メソッドを使用できます。 セクションにButtonタグを設定し、彼の行動に呼び出す:

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

co0o0olさんの返信ありがとうございました。私は本当に本当に本当に働きます。:-) –

関連する問題