为表中的每个部分添加标题标题

时间:2012-02-21 09:03:22

标签: iphone uitableview

我在表格视图中有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";
}

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:2)

你算了几节吗?

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

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

答案 2 :(得分: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;
}

否则一切都是正确的