2011-04-11 49 views
7

我在我的UITableView中有四个部分,如何为每个部分添加标题? 我正在使用下面的代码,但它不工作。如何为表格中的每个部分添加标题标题

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

回答

8

您确定您已经实施tableView:heightForHeaderInSection:

在你的情况下,标题实际上已设置,但标题的高度默认为0。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { 
    return 40; //play around with this value 
} 

PS:考虑这样的方法

+0

什么是'@ switch',它与switch'有什么不同? – Richard 2011-04-11 15:27:22

+0

我相信它们是一样的东西...... – matteodv 2011-04-11 16:37:29

+0

没有'@ switch'。我认为这是一个错字。 ;) – 2011-04-11 16:48:25

-4

更改您的if语句这样

if(section == 0) 
    return @"ABC"; 
else if (section == 1) 
    return @"XYZ"; 
else 
    return @"PQR" 

我希望这有助于。请让我知道,如果我可以有任何帮助。

Pinku :)

+0

更好地使用开关() – 2013-07-17 13:25:33

相关问题