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

其他一切都是正確的