headerViewForSection:如果在iphone上看不到Header View或section,则返回nil

时间:2015-02-03 10:59:27

标签: ios objective-c uitableview reloaddata sectionheader

我正在使用我在查看numberOfRowsInSection:方法时添加到标题视图中的UIButton,并且在按钮状态下我正在检查该部分中是否存在行。但我的应用程序崩溃了 [self.tableView headerViewForSection:section]返回nil。由于它对于可见的部分工作正常,我假设发生了这种情况,因为此时此部分不可见。

以下是我创建节标题视图的方法:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];;
if(!myHeader) {
    myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
}
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
sectionHeaderView.tag = 1001;
sectionHeaderView.backgroundColor = [UIColor whiteColor];
NSDictionary *sectionDict = [self.sectionArray objectAtIndex:section];
NSString *sectionName = [sectionDict objectForKey:@"name"];

if ([sectionName isEqualToString:@"Verified Seller"]) {

    UIImageView *sectionHeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 44, 44)];
    sectionHeaderImageView.image = [UIImage imageNamed:@"blue_ok.png"];
    [sectionHeaderImageView setContentMode:UIViewContentModeCenter];
    [sectionHeaderView addSubview:sectionHeaderImageView];

    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(74, 5, 200, 34)];
    sectionHeaderLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
    sectionHeaderLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:16];
    sectionHeaderLabel.text = @"Verified Seller";
    [sectionHeaderView addSubview:sectionHeaderLabel];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 43, 305, 1)];
    view.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view];
}
else if ([sectionName isEqualToString:@"Pro-Seller"]){
    UIImageView *sectionHeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 44, 44)];
    sectionHeaderImageView.image = [UIImage imageNamed:@"blue_ok.png"];
    [sectionHeaderImageView setContentMode:UIViewContentModeCenter];
    [sectionHeaderView addSubview:sectionHeaderImageView];

    UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(74, 5, 200, 34)];
    sectionHeaderLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
    sectionHeaderLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:16];
    sectionHeaderLabel.text = @"Pro Seller";
    [sectionHeaderView addSubview:sectionHeaderLabel];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 43, 305, 1)];
    view.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view];
}
else if ([sectionName isEqualToString:@"Diagnostic"]){
    UIButton *diagnosticBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [diagnosticBtn setTitle:@"Diagnostic Report" forState:UIControlStateNormal];
    [diagnosticBtn setTitle:@"Diagnostic Report" forState:UIControlStateSelected];
    [diagnosticBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [diagnosticBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];


    [diagnosticBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
    [diagnosticBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
    [diagnosticBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
    [diagnosticBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
    [diagnosticBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -170, 0, 0)];
    diagnosticBtn.tag = section;
    [diagnosticBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeaderView addSubview:diagnosticBtn];

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
    view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Certification"]){
    UIButton *certificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [certificationBtn setTitle:@"Certification" forState:UIControlStateNormal];
    [certificationBtn setTitle:@"Certification" forState:UIControlStateSelected];
    [certificationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [certificationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];


    [certificationBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
    [certificationBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
    [certificationBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
    [certificationBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
    [certificationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -210, 0, 0)];
    certificationBtn.tag = section;
    [certificationBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeaderView addSubview:certificationBtn];

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
    view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Seller Declaration"]){
    UIButton *sellerDeclarationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [sellerDeclarationBtn setTitle:@"Seller Declaration" forState:UIControlStateNormal];
    [sellerDeclarationBtn setTitle:@"Seller Declaration" forState:UIControlStateSelected];
    [sellerDeclarationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [sellerDeclarationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];


    [sellerDeclarationBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
    [sellerDeclarationBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
    [sellerDeclarationBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
    [sellerDeclarationBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
    [sellerDeclarationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -170, 0, 0)];
    sellerDeclarationBtn.tag = section;
    [sellerDeclarationBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeaderView addSubview:sellerDeclarationBtn];
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
    view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Others"]){
    UIButton *otherTrustFactorsBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [otherTrustFactorsBtn setTitle:@"Other Trust Factor" forState:UIControlStateNormal];
    [otherTrustFactorsBtn setTitle:@"Other Trust Factor" forState:UIControlStateSelected];
    [otherTrustFactorsBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
    [otherTrustFactorsBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];


    [otherTrustFactorsBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
    [otherTrustFactorsBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
    [otherTrustFactorsBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
    [otherTrustFactorsBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
    [otherTrustFactorsBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -165, 0, 0)];
    otherTrustFactorsBtn.tag = section;
    [otherTrustFactorsBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    [sectionHeaderView addSubview:otherTrustFactorsBtn];

    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
    view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
    [sectionHeaderView addSubview:view1];
}

[myHeader addSubview:sectionHeaderView];
return myHeader;
}

以下是我在numberOfRowsInSection中填充UITableView中数据的方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *sectionSuperDict = [self.sectionArray objectAtIndex:section];
NSString *sectionName = [sectionSuperDict objectForKey:@"name"];
NSDictionary *sectionDict = [sectionSuperDict objectForKey:@"value"];
UIView *sectionHeaderView = [self.tableView headerViewForSection:section];
UIButton *button;
for (UIView *subView in sectionHeaderView.subviews) {
    if (subView.tag == 1001) {
        for (UIView *subSubView in subView.subviews) {
            if ([subSubView isKindOfClass:[UIButton class]]) {
                button = (UIButton *)subSubView;
            }
        }
    }

}

NSInteger optionsCount = 0;

if ([sectionName isEqualToString:@"Verified Seller"]) {
}
else if ([sectionName isEqualToString:@"Pro-Seller"]){
}
else if ([sectionName isEqualToString:@"Diagnostic"]){
    if (!button.selected) {
        optionsCount = 0;
    }
    else{
        if ([sectionDict isKindOfClass:[NSArray class]]) {
            optionsCount = 1;
        }
        else{
            NSString *documentSource = [sectionDict objectForKey:@"document_source"];
            if ([documentSource isEqualToString:@"none"]) {
                optionsCount = 1;
            }
            else{
                optionsCount = 3;
                if ([sectionDict objectForKey:@"documents"]) {
                    optionsCount +=1;
                }

                if ([sectionDict objectForKey:@"document_note"]) {
                    if (![[sectionDict objectForKey:@"document_note"] isEqualToString:@""]) {
                        optionsCount +=1;
                    }

                }
            }
        }
    }
}
else if ([sectionName isEqualToString:@"Certification"]){
    if (!button.selected) {
        optionsCount = 0;
    }
    else{
        if ([sectionDict isKindOfClass:[NSArray class]]) {
            optionsCount = 1;
        }
        else{
            NSString *documentSource = [sectionDict objectForKey:@"document_source"];
            if ([documentSource isEqualToString:@"none"]) {
                optionsCount = 1;
            }
            else{
                optionsCount = 3;
                if ([sectionDict objectForKey:@"documents"]) {
                    optionsCount +=1;
                }

                if ([sectionDict objectForKey:@"document_note"]) {
                    if (![[sectionDict objectForKey:@"document_note"] isEqualToString:@""]) {
                        optionsCount +=1;
                    }

                }
            }
        }

    }
}
else if ([sectionName isEqualToString:@"Seller Declaration"]){
    if (!button.selected) {
        optionsCount = 0;
    }
    else{
        if ([sectionDict isKindOfClass:[NSDictionary class]]) {
            NSDictionary *valueDict = [sectionDict objectForKey:@"value"];
            if ([valueDict isKindOfClass:[NSDictionary class]]) {
                if ([[valueDict allKeys] count]) {
                    NSDictionary *acceptDict = [valueDict objectForKey:@"accept"];
                    if ([acceptDict isKindOfClass:[NSDictionary class]]) {
                        if ([[acceptDict allKeys] count]) {
                            NSString *acceptLabelString = [acceptDict objectForKey:@"label"];
                            if (![acceptLabelString isEqualToString:@""]) {
                                optionsCount +=1;
                            }
                        }
                    }

                    NSDictionary *declarationsDict = [valueDict objectForKey:@"declarations"];
                    if ([declarationsDict isKindOfClass:[NSDictionary class]]) {
                        optionsCount = optionsCount + [[declarationsDict allKeys] count];
                    }
                }
                else{
                    optionsCount = 1;
                }
            }
            else{
                optionsCount = 1;
            }
        }
        else{
            optionsCount = 1;
        }
    }
}
else if ([sectionName isEqualToString:@"Others"]){
    if (!button.selected) {
        optionsCount = 0;
    }
    else{
        optionsCount = 1;
        NSDictionary *serviceLogDict = [sectionDict objectForKey:@"service_log"];
        if ([serviceLogDict isKindOfClass:[NSDictionary class]]) {
            if ([[serviceLogDict allKeys] count]) {
                //optionsCount +=1;
            }
        }

        NSDictionary *registrationDict = [sectionDict objectForKey:@"registration"];
        NSDictionary *insuranceDict = [sectionDict objectForKey:@"insurance"];

        if ([registrationDict isKindOfClass:[NSDictionary class]] && [insuranceDict isKindOfClass:[NSDictionary class]]) {
            if ([[registrationDict allKeys] count] || [[insuranceDict allKeys] count]) {
                optionsCount +=1;
            }

        }
    }
}

return optionsCount;
}

由于按钮在此处返回nil,而在更新rowCount为第5节时为1之前,它会抛出异常。应该怎么做才能

[self.tableView headerViewForSection:section]

对于隐形部分,不会返回nil吗?

2 个答案:

答案 0 :(得分:0)

这不是您问题的答案,但它可能是您的目的的解决方法:
我会为sectionArray中的项添加键值对(例如:"visible" = "true/false/0/1/...")。在部分的按钮操作中修改此值。然后,阅读numberOfRowsInSection...方法中每个项目的值,以便返回0sectionArray.count,具体取决于该部分当前是否可见。
希望它有所帮助。

答案 1 :(得分:0)

在各个部分的任意位置访问表视图标题的快速解决方案

let tblHeaderView = tableView(YourTableView, viewForHeaderInSection: 0)