1

我正在开发一个类似于联系人应用程序的应用程序。它有一个项目列表,每个项目都有一个详细视图。详细视图是一个表格视图,显示名称、地点、值等值。但是,如果地点没有值,我不希望它的部分出现,这样详细视图中就没有空白单元格. 我有6个部分。第 2 部分应始终显示,即使未分配值且第 5 部分有两行。所有其他部分都有 1 行。我看过其他问题;但是,我并没有取得太大的成功。我当前的代码如下所示:

- (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”而终止应用程序,原因:“ * -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]”

如果有人能给我一些建议或让我朝着正确的方向前进,我将不胜感激!非常感谢!斯科特

4

0 回答 0