2013-10-08 2 views
5

뷰 컨트롤러에 tableview를 추가 한 다음 IOS 6.0에서 섹션 헤더 제목을 변경하려고합니다.UITableView titleForHeaderInSection을 호출하지 않습니다.

내가 헤더 문자열을 특정 기능이 그래서 내가 (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section을 사용하려고하지만 중단 점을 넣을 때이 호출되지 않는 것을 볼 수 (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

처리 할 해달라고 호출 될 때마다 시간을 변경합니다. 내가 UITableViewDelegate를 추가

in.h 파일, UITableViewDataSource

in.m 내가 잘못 뭐하는 거지

-(void)viewDidAppear:(BOOL)animated 
{ 
    //tableview init 
    self.meetingList=[[UITableView alloc] initWithFrame:CGRectMake(10, self.ckCal.frame.origin.y + self.ckCal.bounds.size.height+20, 384, 450) style:UITableViewStylePlain]; 
    [self.meetingList setSeparatorStyle:UITableViewCellSeparatorStyleNone]; 
    self.meetingList.delegate=self; 

    //populate mutablearray for tableview here 
    [self eventsInThisMonth:[NSDate date]]; 

    [self.view addSubview:self.meetingList]; 
} 
-(void)eventsInThisMonth:(NSDate *)date 
{ 

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"MMMM , yyyy"]; 
    //self.firstSectionHeader=nil; 
    self.firstSectionHeader= [NSString stringWithFormat:@"Events in %@", [dateFormatter stringFromDate:date]]; 
    NSLog(@" self.firstSectionHeader %@ ",self.firstSectionHeader); 

} 
#pragma Tableview 
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return [self.eventHeaders count] + 1; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 35; 
} 
//set header section labels 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 35)] ; 
    [headerView setBackgroundColor:[UIColor colorWithRed:106/256.0 green:106/256.0 blue:106/256.0 alpha:1.0]]; 
    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(92, 10, tableView.bounds.size.width, 20)]; 
    subjectLabel.textColor = [UIColor whiteColor]; 
    subjectLabel.font = [UIFont fontWithName:@"Gill Sans" size:20]; 
    subjectLabel.backgroundColor = [UIColor clearColor]; 
    subjectLabel.text=self.firstSectionHeader; 
    NSLog(@"subjectLabel.text %@",subjectLabel.text); 

    if (section==0) { 

     //[headerView addSubview:subjectLabel]; 
     return headerView; 
    } 
    else 
     return nil; 


} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if (section==0) { 
     NSLog(@"self.firstSectionHeader in titleForHeaderInSection %@",self.firstSectionHeader); 
     return self.firstSectionHeader; 
    } 
    else 
     return nil; 
} 

?

+0

코드를 보면 ... 왜 viewForHeaderInSection과 titleForHeaderInSection을 모두 사용하고 싶습니까? 어쨌든 viewForHeaderInSection의 제목을 설정하고 있습니다 ... – Odrakir

답변

15

아마도 두 가지 중 하나, 즉 viewForHeaderInSection 또는 titleForHeaderInSection 중 하나만 사용해야하기 때문일 수 있습니다.

+0

헤더 섹션의 배경색을 변경하려면 어떻게해야합니까? – user2858870

+0

viewForHeaderInSection에 새로운 헤더를 만들어야합니다. – Odrakir

+1

둘 다 구현할 수 있습니다. viewForHeaderInSection있는 UILabel (어떤 NSString 당신이 제목을 사용하고 보유하고있다) 있다고 가정합니다. titleForHeaderInSection을 호출하여 해당 문자열을 설정하면됩니다. 두 세계 모두에서 가장 좋습니다. – Doc

1

당신은 당신의 ViewController는 UITableViewDelegate과 UITableViewDataSource를 implemente하는 것을 기억

self.meetingList.datasource = self;가 누락되었습니다.

YourClass : UITableView <UITableViewDelegate, UITableViewDataSource> 
+0

당신의 대답은 반정도 맞습니다 감사합니다, 나는 당신의 대답을 upvote하려고했지만 충분한 명성없이 upvote 수 없습니다. – user2858870

+0

너무 나빠 보이네. ( – artud2000

0

당신은 viewDidAppear에

self.meetingList.dataSource=self; 

을해야한다.

1

Re : 내가 뭘 잘못하고있어.

데이터 소스를 설정하고 인터페이스 작성기에서보기 컨트롤러에 위임을 설정했는지 확신 할 수 없습니다. 그러나 예상되는 결과에 영향을주는 논리 오류가 있다고 생각합니다.

titleForHeaderInSection : 테이블 뷰의 지정된 섹션의 머리글 제목에 대한 데이터 원본을 요구합니다.

한편

있는 tableView : viewForHeaderInSection : 는 뷰 객체가 테이블 뷰의 특정 섹션의 헤더에 표시 할 위임을 요청한다.

후자의 방법은 titleForHeaderInSection에 정의 된 동작을 무시합니다. 이것을 이렇게 생각하십시오. 제목을 보유 할 수있는 하나의 헤더 공간 또는 기본 (제목)을 대체하기 위해 작성하는 사용자 정의보기가 있습니다. 두 번째 방법을 구현하여 사용자 지정보기를 사용하도록 컴파일러에 지시하고 있습니다. 이 주석을 수정하거나 tableView : viewForHeaderInSection : 메소드를 제거하거나 로직을 변경하여 메소드에 전달한 사용자 정의보기의 제목을 업데이트하십시오. 이 시점에서 titleForHeaderInSection을 정확하게 맞추어야합니다.

처음 질문을 성공적으로 해결 한 경우이 대답을 수락하는 것이 좋습니다.

0

나는이 스레드는 좀 오래 알고 있지만, 당신은 또한 가지고 있는지 확인 : 그것은보기를 잡기 전에

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

있는 tableview의 높이를 알아야합니다.

0

또한, 당신은 당신이 다음 중 하나 이상 첫 번째 방법을 구현하는 경우 모두

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

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

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

를 호출되지 않습니다 구현하지 필요 .

관련 문제