Objective c 隐藏/删除表视图节

Objective c 隐藏/删除表视图节,objective-c,uitableview,row,Objective C,Uitableview,Row,我正在开发一个类似于联系人应用程序的应用程序。它有一个项目列表,每个项目都有一个详细视图。详图视图是一个表视图,显示名称、位置、值等值。但是,如果位置没有值,我不希望它的部分出现,以便详图视图中没有空白单元格。我有6个部分。即使未指定值,第2节也应始终显示,第5节有两行。所有其他部分都有一行。 我看了其他问题;然而,我并没有取得多少成功。我当前的代码如下所示: - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

我正在开发一个类似于联系人应用程序的应用程序。它有一个项目列表,每个项目都有一个详细视图。详图视图是一个表视图,显示名称、位置、值等值。但是,如果位置没有值,我不希望它的部分出现,以便详图视图中没有空白单元格。我有6个部分。即使未指定值,第2节也应始终显示,第5节有两行。所有其他部分都有一行。 我看了其他问题;然而,我并没有取得多少成功。我当前的代码如下所示:

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return sectionTitles;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        if ([[[detailGift givingTo] name] length] == 0) {
            return nil;
        }
        else {
            return @"";
        }
    }
    else if (section == 2) {
        return @"";
    }
    else if (section == 3) {
        if ([[[detailGift reasonFor] name] length] == 0) {
            return nil;
        }
        else {
            return @"";
        }
    }
    else if (section == 4) {
        if ([[[detailGift boughtFrom] name] length] == 0) {
            return nil;
        }
        else {
            return @"";
        }
    }
    else if (section == 5) {
        if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
            return nil;
        }
        else {
            return @"";
        }
    }
    else {
        if ([[detailGift notes] length] == 0) {
            return nil;
        }
        else {
            return @"";
        }
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        if ([[[detailGift givingTo] name] length] == 0) {
            return 0;
        }
        else {
            return 1;
        }
    }
    else if (section == 2) {
        return 1;
    }
    else if (section == 3) {
        if ([[[detailGift reasonFor] name] length] == 0) {
            return 0;
        }
        else {
            return 1;
        }
    }
    else if (section == 4) {
        if ([[[detailGift boughtFrom] name] length] == 0) {
            return 0;
        }
        else {
            return 1;
        }
    }
    else if (section == 5) {
        if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
            return 0;
        }
        else if ([[formatter stringFromDate:[detailGift dateGiving]] length] != 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] == 0) {
            return 1;
        }
        else if ([[formatter stringFromDate:[detailGift dateGiving]] length] == 0 && [[formatter stringFromDate:[detailGift datePurchased]] length] != 0) {
            return 1;
        }
        else {
// Crashes after returning value of two
            return 2;
        }
    }
    else {
        if ([[detailGift notes] length] == 0) {
            return 0;
        }
        else {
            return 1;
        }
    }
}
这对我来说似乎非常混乱,并且导致以下异常: 由于未捕获异常“NSRangeException”而终止应用程序,原因:“*-[\uu NSArrayI objectAtIndex:]:索引1超出边界[0..0]”

如果有人能给我一些建议或给我正确的方向,我会非常感激! 谢谢!
Scott

能否在源代码列表中添加注释,以说明引发错误的位置?我假设你已经用调试器完成了,但是如果没有,那是我要重新推荐的第一件事。错误很明显,你正在处理一个空数组,就好像它不是空的一样。只需记录每个数组,看看它是否为空。顺便说一下,部分从0开始,在这种情况下,“else”将填充第一个部分section@RickvanderLinde啊!我相信这就是问题所在。我的tableview有一定数量的分区,所以我可能试图访问一个不存在的分区。非常感谢。