2012-02-21 90 views
2

我在表格视图中有4个部分如何为每个部分添加标题我正在编写follwing代码,但它不工作。为表格中的每个部分添加标题标题

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

其他一切都是正确的