2012-06-27 17 views
1

我在改變TableView上的顏色和字體時遇到了一些麻煩,我已經將它們分成了4個部分,名稱等,我似乎可以使它工作我不知道我在做什麼錯了?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 sectionName; 

這是你應該返回什麼..

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委託方法。你的代碼返回NSString,它應該返回UIView。

+0

請解釋爲什麼此代碼與問題中的代碼不同/更好。僅由代碼組成的答案通常不是一個好答案。 – jrturton

+0

是的,你可以解釋一下代碼嗎?我已經試過了,現在需要關閉部分名稱!謝謝 –

+0

@NathanCleary請在方法中設置高度 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection :(NSInteger)部分 – Neo

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

是在UITableView標題中呈現視圖的正確方法。在這裏你可以返回你的UILabel對象。

您試圖返回一個NSString對象而不是UILabel。返回類型的方法也是錯誤的。它應該是UIView類型的。

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

+0

你在乎解釋..? –

+0

我的答案格式不正確。對不起。 – Neo