2012-02-21 12 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; 
} 

関連する問題