2012-02-21 2 views
2

내 테이블보기에 4 개의 섹션이 있습니다. 모든 섹션의 머리글을 추가하는 방법은 다음과 같습니다. 그러나 그 코드는 작동하지 않습니다.표의 각 섹션에 대한 머리글 제목을 추가하십시오.

코드에 따라
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section { 
    if (section == 0) 
     return @"Tasks"; 

if (section == 1) 
    return @"Appointments"; 

if (section == 2) 
    return @"Activities"; 

if (section == 3) 
    return @"Inactivities"; 
} 

답변

4

사용 ..

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

    UILabel *lbl = [[UILabel alloc] init]; 
    [lbl setBackgroundColor:[UIColor clearColor]]; 
    [lbl setFont:[UIFont fontWithName:@"Arial" size:17]]; 
    [lbl setTextColor:BROWN]; 
    switch (section) 
    { 
    case 0: 
     lbl.text = @" Name"; 
     break; 
    case 1: 
     lbl.text = @" Quantity"; 
     break; 
    case 2: 
     lbl.text = @" Amount"; 
     break; 
    } 

    return lbl; 
} 
2

당신이 섹션을 계산 했습니까? 섹션의

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 

return [sections count]; //or 4 in your case 
} 
1

수표 번호는 4 아니고,이 코드를 변경 : 모든 옳다 다른

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:   (NSInteger)section 
{ 
    NSString *returnValue = @""; 
  if (section == 0) 
        returnValue = @"Tasks"; 
    else if (section == 1) 
    returnValue = @"Appointments"; 
    else if (section == 2) 
    returnValue = @"Activities"; 
    else if (section == 3) 
   returnValue = @"Inactivities"; 
return returnValue; 
} 

관련 문제