2011-09-12 4 views
6

개별 섹션이있는 색인 된 UITableView가 있습니다. 각 섹션의 헤더보기에 다른 배경색을 사용하고 싶습니다. tableView : viewForHeaderInSection :을 구현하여 내 자신의 뷰를 완전히 롤업 할 수 있다는 것을 알고 있습니다 (예 : question # 2898361 참조).하지만 나에게 너무 많은 작업 인 것처럼 보입니다. 표준 뷰가 멋지게 보이지만 배경을 변경해야합니다. 색깔.tableView의 표준 viewForHeaderInSection에 어떻게 액세스합니까?

하지만이 표준보기에 어떻게 액세스합니까? [super tableView:viewForHeaderInSection:]을 사용할 수 없습니다. 프로토콜을 구현할 때의 문제이며 상속 문제가 아니기 때문입니다. 다른 방법으로 표준보기를 얻을 수 있습니까?

답변

13

나는 이것을 거의 쉽게 할 수 없다. 나는 나의 기술 지원 요청 중 하나를 최근 devut 계정에 사용하여 UITableView 섹션의 배경과 테두리를 변경하는 것에 대해 최근에 물었다. 사과 엔지니어는이 작업이 실제로는 쉬운 일이 아니라고 말했고, 수행 한 경우에도 성능에 영향을 미칠 수 있다고 말했습니다. 그는 또한 cocoawithlove 저를 지적하고 편집 uitableviews에 대한 기사 :

http://cocoawithlove.com/2009/08/adding-shadow-effects-to-uitableview.html

정말, 자신의 헤더를 만드는 것은 그 많은 노력이 아니다. 그것은 주석, 그래서 바로 작동하지 않을 수 있습니다 - - 아래는 내 프로젝트 중 하나에서 철수 일부 코드는하지만 당신은 아이디어를 얻을 수 있습니다 :

- (CAGradientLayer *) greyGradient { 
    CAGradientLayer *gradient = [CAGradientLayer layer]; 
    gradient.startPoint = CGPointMake(0.5, 0.0); 
    gradient.endPoint = CGPointMake(0.5, 1.0); 

    UIColor *color1 = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0]; 
    UIColor *color2 = [UIColor colorWithRed:240.0f/255.0f green:240.0f/255.0f blue:240.0f/255.0f alpha:1.0]; 

    [gradient setColors:[NSArray arrayWithObjects:(id)color1.CGColor, (id)color2.CGColor, nil]]; 
    return gradient; 
} 

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    CGFloat width = CGRectGetWidth(tableView.bounds); 
    CGFloat height = [self tableView:tableView heightForHeaderInSection:section]; 
    UIView *container = [[[UIView alloc] initWithFrame:CGRectMake(0,0,width,height)] autorelease]; 
    container.layer.borderColor = [UIColor grayColor].CGColor; 
    container.layer.borderWidth = 1.0f; 
    CAGradientLayer *gradient = [self greyGradient]; 
    gradient.frame = container.bounds; 
    [container.layer addSublayer:gradient]; 

    UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(12,0,width,height)] autorelease]; 
    headerLabel.backgroundColor = [UIColor clearColor]; 
    headerLabel.font= [UIFont boldSystemFontOfSize:19.0f]; 
    headerLabel.shadowOffset = CGSizeMake(1, 1); 
    headerLabel.textColor = [UIColor whiteColor]; 
    headerLabel.shadowColor = [UIColor darkGrayColor]; 
    NSString *title = [self tableView:tableView titleForHeaderInSection:section]; 
    headerLabel.text = title; 
    return container; 
} 

확인

#import <QuartzCore/QuartzCore.h> 

에 작성자 방법 ... 표준 헤더의 모양을 모방하지 않아도됩니다. 하지만 약간의 시행 착오로 표준을 모방 한 다음 색을 약간 바꿀 수 있다고 확신합니다.

+1

텍스트에 뭔가 빠져있는 것 같습니다. #import what? 아마도 핵심 애니메이션 물건일까요? – Thorsten

+0

오, 네, CAGradientLayer를 사용하기위한 QuartzCore였습니다 - 게시에 추가하겠습니다. 감사합니다 – bandejapaisa

+0

또한 [container addSubview : headerLabel]이 누락되었습니다. 다음과 같이하면 더 좋습니다. \t CGFloat width = tableView.bounds.size.width; \t CGFloat height = [self tableView : tableView heightForHeaderInSection : section]; UIView * container = [[UIView alloc] initWithFrame : CGRectMake (0,0, width, height)]; –

1

@bandejapalsa 솔루션에는 한 가지 문제가 있습니다. 이전 셀의 구분 기호가 기본 구현 섹션의 HeaderView에없는 구현 위치에서 계속 표시됩니다. 내가 찾은 해결책은 CALayer를 사용하고 1 pix만큼 오프셋하는 것이 었습니다. 이미지는 뷰 프레임 자체보다 1pix 더 커야합니다.

// Create the view for the header 
CGRect aFrame =CGRectMake(0, 0, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight); 
UIView * aView = [[UIView alloc] initWithFrame:aFrame]; 
aView.backgroundColor = UIColor.clearColor; 

// Create a stretchable image for the background that emulates the default gradient, only in green 
UIImage *viewBackgroundImage = [[UIImage imageNamed:@"greenheader.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 

// Cannot set this image directly as the background of the cell because 
// the background needs to be offset by 1pix at the top to cover the previous cell border (Alex Deplov's requirement ^_^) 
CALayer *backgroungLayer = [CALayer layer]; 

backgroungLayer.frame = CGRectMake(0, -1, tableView.contentSize.width, IMIUICustomisation.sectionHeaderViewHeight+1); 
backgroungLayer.contents = (id) viewBackgroundImage.CGImage; 
backgroungLayer.masksToBounds = NO; 
backgroungLayer.opacity = 0.9; 
[aView.layer addSublayer:backgroungLayer]; 

// Take care of the section title now 
UILabel *aTitle = [[UILabel alloc] initWithFrame: CGRectMake(10, 0, aView.bounds.size.width-10, aView.bounds.size.height)]; 
aTitle.text = [delegate tableView:tableView titleForHeaderInSection:section]; 
aTitle.backgroundColor = UIColor.clearColor; 
aTitle.font = [UIFont boldSystemFontOfSize:18]; 
aTitle.textColor = UIColor.whiteColor; 

// Text shadow 
aTitle.layer.shadowOffset = CGSizeMake(0, 1); 
aTitle.layer.shadowRadius = .2; 
aTitle.layer.masksToBounds = NO; 
aTitle.layer.shadowOpacity = 0.5; 
aTitle.layer.shadowColor = IMIUICustomisation.selectedElementTextShadowColor.CGColor; 
[aView addSubview:aTitle]; 

return aView; 
5

다른 답변이 제대로 지적하고 있지만 당신이 그것을 간단한 수정을 위해 기본보기에 액세스 할 수없는 특정 섹션 헤더에 대한 사용자 정의 아무 상관이없는 경우, 당신은 tableView:viewForHeaderInSection:에서 nil을 반환 할 수 있습니다 및 테이블 뷰 것 기본보기를 사용하십시오.

일부 헤더 만 사용자 정의하면 도움이됩니다.

어떤 이유로이 값은 undocumented입니다.

관련 문제