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

еще все верно

Смежные вопросы