2012-09-18 2 views
0

그룹화 된 tableview의 머리글에 표시 할 문자열이 3 개 있습니다. 문제는이 문자열을 tableView titleForHeaderInSection에 추가하고 각 문자열을 다른 색과 글꼴로 다른 줄에 표시하고 싶습니다.그룹화 된 머리글의 단어와 글꼴이 다릅니다. UItableview

현재 내가 가지고있는 것은 :

enter image description here

내가 원하는 것은 :

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

    NSString * presenter = [[self agenda] getBriefingPresenter:section]; 
    NSString * time = [[self agenda] getBriefingTime:section]; 
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"]; 

    return [NSString stringWithFormat:@"%@\n%@ %@", subject, time, presenter ]; 

} 

방법을 다음과 같은

enter image description here

내가 얻고 그 세 문자열을 추가 머리를 다루는 어 글꼴

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

    NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section]; 
    if (sectionTitle == nil) { 
     return nil; 
    } 

    // Create label with section title 
    UILabel *label = [[UILabel alloc] init]; 
    label.frame = CGRectMake(45, 0, 484, 23); 
    label.textColor = [UIColor whiteColor]; 
    label.font = [UIFont fontWithName:@"Century Gothic" size:17]; 
    label.text = sectionTitle; 
    label.lineBreakMode = UILineBreakModeWordWrap; 
    label.numberOfLines = 2; 
    [label sizeToFit]; 
    label.backgroundColor = [UIColor clearColor]; 

    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 320, 400)]; 
    [view addSubview:label]; 

    return view; 
} 

그래서이 sectionTitle을 분석하고 세 줄에 세 가지 다른 레이블로 인쇄 할 수 있습니까?

답변

2

(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section에는 특정 글꼴 및 색상으로 3 가지 라벨을 만듭니다. 하나의 UILabel에 다른 글꼴/색상을 설정할 수 없습니다.

은 그래서 코드가 업데이트 :

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

    NSString * presenter = [[self agenda] getBriefingPresenter:section]; 
    NSString * time = [[self agenda] getBriefingTime:section]; 
    NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:@"subject"]; 

    // Create label with section title 
    UILabel *presenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 484, 23)]; 
    presenterLabel.textColor = presenterColor; 
    presenterLabel.font = presenterFont; 
    presenterLabel.text = presenter; 
    presenterLabel.backgroundColor = [UIColor clearColor]; 
    [presenterLabel sizeToFit]; 

    UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, presenterLabel.frame.size.height, 484, 23)]; 
    timeLabel.textColor = timeColor; 
    timeLabel.font = timeFont; 
    timeLabel.text = time; 
    timeLabel.backgroundColor = [UIColor clearColor]; 
    [timeLabel sizeToFit]; 

    UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, timeLabel.frame.origin.y + timeLabel.frame.size.height, 484, 23)]; 
    subjectLabel.textColor = subjectColor; 
    subjectLabel.font = subjectFont; 
    subjectLabel.text = subject; 
    subjectLabel.backgroundColor = [UIColor clearColor]; 
    [subjectLabel sizeToFit]; 


    // Create header view and add label as a subview 
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)]; 
    [view addSubview:presenterLabel]; 
    [view addSubview:timeLabel]; 
    [view addSubview:subjectLabel]; 

    return view; 
} 

당신은 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section

+0

은 감사하지만, 필요하지 않습니다 나는'제거 할 때 - (있는 NSString *)있는 tableView : (jQuery과 *)있는 tableView titleForHeaderInSection : (NSInteger를) section' 제목 또는 머리글 영역이 0으로 줄어들고 간격이 없습니다. 그룹화 된 tableviews –

+0

'- (CGFloat) tableView : (UITableView *) tableView heightForHeaderInSection : (NSInteger) section'의 서브 클래스를 만들고 적절한 높이를 반환하면됩니다. – nicolasthenoz

+0

그냥 추가 '- (NSString *) tableView : (UITableView *) tableView titleForHeaderInSectio n : (NSInteger) 섹션 { return [NSString stringWithFormat : @ "\ n \ n \ n"]; }'작동하지만 나에게 좋은 해결책인지 모르겠다. –