본문 바로가기

개발도구/iOS - 아이폰 개발

[iso] tableview Header

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 55;

}

Step 2: create & return the customized section header.

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

}

Step 3: return number of sections. ( for example 10 here )

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 10;

}

Step 4 : number of rows per section. ( for example, 4 rows for each section )

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 4;

}

Step 5 : create & return cell (UITableViewCell for each Row)

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

}

Step 6: add the event to handle the TouchDown on section header.

- (void)sectionTapped:(UIButton*)btn {

    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:btn.tag-1] atScrollPosition:UITableViewScrollPositionTop animated:YES];

}