Pooja
Pooja

Reputation: 85

add header title for every Section in Table

I have 4 section in my table view how to add header for every section i am writing follwing code but its not working.

- (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";
}

Upvotes: 2

Views: 159

Answers (3)

Rupesh
Rupesh

Reputation: 2051

Use Following Code..

-(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;
}

Upvotes: 4

kulss
kulss

Reputation: 2055

check number of section is 4 or not and change this code into:

- (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;
}

else all are right

Upvotes: 1

alecnash
alecnash

Reputation: 1768

Did you count the sections?

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

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

Upvotes: 2

Related Questions