본문 바로가기

iPhone

UITableView Header와 SectionHeader custom 변경

테이블 뷰를 보면 header을 설정 할 수 있는데 header는 Header와 SectionHeader으로 구분된다.

여기서 Header은 TableView의 tableHeaderView를 말하며 최상단에 항상 붙어있고 SectionHeader는 영역을 나누어주는 Section을 말한다.




Header을 변경 또는 설정하려면 tableView의 프로퍼티로 선언된 tableHeaderView를 설정해 주면된다. 

tableHeaderView는 UIView타입 이기 때문에 header을 UIiView로 만들어 넣어주면 해당뷰는 최상단에 붙어 나오게 된다.


UITableView *table;

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 39)];   //뷰를 만들고

// 배경으로 깔릴 이미지가 필요하다면 만들어준다.

    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 39)]; 

    bgImageView.image = [UIImage imageNamed:@"title_tw.png"];

    //헤더에 labl이 필요하다면 만들어준다.

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(118, 9, 84, 24)];

    title.textColor = [UIColor whiteColor];

    title.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];

    title.text = @"Friends List";

    [headerView addSubview:bgImageView];  //만들어진 뷰에 배경과 라벨을 넣어준다

    [headerView addSubview:title];


    table.tableHeaderView = headerView; // 해더에 세팅


view는 코드로 직접 만들지 않고 xib를 사용하여 만들어 붙여도 무방하다.

header뿐만 아니라 footer을 설정하고 싶은 경우도 tableView의 프로토콜로 선언된 tableFooterView를 변경해 주면 된다.



SectionHeader는 tableView에서 섹션을 설정 하였을경우 섹션의 상단에 붙어있는 부분이다. 보통 a,b,c등과 같이 구분되는 lable을 보여준다.

SectionHeader를 커스텀 하려면 tableView의 델리게이트 메서드인 

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

에서 설정해 주어야 한다. 만약 view의 커스텀이 아니라 단지 보이는 label의 값만 바꾸고 싶다면 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; 에서 세팅해 주면된다.


lable만 바꿀 경우

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return @"바꾸고 싶은 String";

}


footer는 - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

를 사용하면 된다.


SectionHeader custom의 경우

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

{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)];

    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)];

    bgImageView.image = [UIImage imageNamed:@"list_title.png"];

    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, 3, 22, 22)];

    title.textColor =[UIColor colorWithRed:((float) 130.0f / 255.0f)

                                     green:((float) 135.0f / 255.0f)

                                      blue:((float) 150.f / 255.0f)

                                     alpha:1.0f];

    title.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];

    //세션의 string를 array에 담아놓고 section으로 구분하여 가져다 세팅한다

    title.text = [self.arrayIndex objectAtIndex:section];  

    [headerView addSubview:bgImageView];

    [headerView addSubview:title];

    

    return headerView;

}

SectionFooter는 

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;   

를 같은 방법으로 사용하면 된다.