如何设置分组表格的节标题文本

6
我想为分组的UITableView的每个部分设置标题文本。
我尝试了这段代码,但出现错误“EXC_BAD_ACCESS”。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView.tag==2)
    {
        if (section == 0)
        {
              return @"test1";
        }
        if (section == 1)
        {
              return @"test2";
        }
    }
}

@Faisal:你在哪一行出现了EXC_BAD_ACCESS(意思是哪一行)? - Jhaliya - Praveen Sharma
我仍然在 int retVal = UIApplicationMain(argc, argv, nil, nil); 处收到“EXC BAD ACCESS”错误。 - user594161
您在模拟器或设备中遇到错误了吗?我确信问题不在上述代码中。您没有保留一个自动释放的对象并在其被释放后引用了它。 - Krishnabhadra
我在模拟器和设备上都遇到了这个问题,即使我只保留方法头部分以及方法内容仅为NSLog,它仍然会崩溃。 - user594161
搞定了,不得已在结尾加上 return 0; 以确保安全失败。 - user594161
尝试使用僵尸来捕获badAccess崩溃。请参阅http://www.codza.com/how-to-debug-exc_bad_access-on-iphone - Krishnabhadra
1个回答

23

搞定了,必须在结尾添加return @"";以确保安全失败。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (tableView.tag==2)
    {
        if (section == 0)
        {
              return @"test1";
        }
        if (section == 1)
        {
              return @"test2";
        }
     }
     return @"";
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接