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

Rup*_*esh 7 iphone objective-c

我在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";
}
Run Code Online (Sandbox Code Playgroud)

knu*_*uku 8

你确定你已经实现了tableView:heightForHeaderInSection : ?

在您的情况下,标题实际上已设置,但标题的高度默认为0.UITableViewDelegate如果您还没有添加此方法:

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

PS:考虑switch在这些方法中使用语句