2012-06-27 3 views
1

색상과 글꼴을 이름 변경 등 4 개의 섹션으로 나눈 TableView에서 변경하는 데 문제가 있습니다. 작동하지 않는 것 같습니다. 내가 잘못하고있는 것이 확실하지 않습니다.sectionName TableView - 내가 뭘 잘못하고 있니?

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

NSString *sectionName = nil; 

switch(section) 
{ 
    case 0: 
     sectionName = [NSString stringWithString:@"Date"]; 
     break; 
    case 1: 
     sectionName = [NSString stringWithString:@"Gig"]; 
     break; 
    case 2: 
     sectionName = [NSString stringWithString:@"City"]; 
     break; 
    case 3: 
     sectionName = [NSString stringWithString:@"Country"]; 
     break; 
} 

UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease]; 
sectionHeader.backgroundColor = [UIColor clearColor]; 
sectionHeader.font = [UIFont boldSystemFontOfSize:18]; 
sectionHeader.textColor = [UIColor whiteColor]; 
sectionHeader.text = sectionName; 

return sectionName; 
} 

답변

4

... ..

return sectionHeader; 
1
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section{ 

NSString *sectionName = nil; 
switch(section) 
{ 
    case 0: 
    sectionName = [NSString stringWithString:@"Date"]; 
    break; 
case 1: 
    sectionName = [NSString stringWithString:@"Gig"]; 
    break; 
case 2: 
    sectionName = [NSString stringWithString:@"City"]; 
    break; 
case 3: 
    sectionName = [NSString stringWithString:@"Country"]; 
    break; 
} 

UILabel *sectionHeader = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)] autorelease]; 
sectionHeader.backgroundColor = [UIColor clearColor]; 
sectionHeader.font = [UIFont boldSystemFontOfSize:18]; 
sectionHeader.textColor = [UIColor whiteColor]; 
sectionHeader.text = sectionName; 
return sectionHeader; 

은}

코드는 어떻게 당신이 필요 달성하기 위해 구현해야 TableViewCOntroller의 viewForHeaderInSection 위임 메서드를 구현하는 방법을 보여줍니다. 귀하의 코드는 UIView를 반환 해야하는 NSString을 반환합니다.

이것은 당신이

return sectionName; 

을 반환하고 이것이 당신이 반환해야 무엇 무엇

대신 문자열의 뷰를 반환해야합니다

+0

이 코드가 질문의 코드와 다른 점/이유를 설명해주십시오. 코드만으로 구성된 대답은 일반적으로 좋은 대답이 아닙니다. – jrturton

+0

예, 코드를 설명해 주시겠습니까? 시도해 보았습니다. 이제 섹션 이름이 뷰에서 사라졌습니다! 감사합니다 –

+0

@ NathanCleary 메서드에서 높이를 설정하십시오 - (CGFloat) tableView : (UITableView *) tableView heightForHeaderInSection : (NSInteger) 섹션 – Neo

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

은 UITableView의 헤더에 뷰를 표시하는 올바른 방법입니다. 여기서 UILabel 객체를 반환 할 수 있습니다.

UILabel 대신 NSString 개체를 반환하려고합니다. 또한 메서드의 반환 형식이 잘못되었습니다. UIView 유형이어야합니다.

http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html

+0

당신은 .. 신경 쓰는 ..? –

+0

내 대답의 형식이 올바르지 않습니다. 그 죄송합니다. – Neo