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

Posted

技术标签:

【中文标题】headerViewForSection:如果 Header View 或 section 在 iphone 上不可见,则返回 nil【英文标题】:headerViewForSection: returns nil if Header View or section is not visible on the iphone 【发布时间】:2015-02-03 10:59:27 【问题描述】:

我正在使用我添加到标题视图的 UIButton 检查 numberOfRowsInSection: 方法,并在按钮状态上检查该部分中是否存在行。但是当我的应用程序崩溃时 [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;

这是我在 UITableView 中的 numberOfRowsInSection 中填充数据的方式:

- (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,而在更新第 5 节的 rowCount 为 1 之前,它会引发异常。应该怎么做才能让

[self.tableView headerViewForSection:section]

不为不可见部分返回 nil 吗?

【问题讨论】:

【参考方案1】:

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

【讨论】:

谢谢你!虽然这不能回答问题,但它是解决问题的合适方法。我实现了类似的东西。非常感谢!【参考方案2】:

在各个部分的任何地方访问表格视图标题的 Swift 解决方案

let tblHeaderView = tableView(YourTableView, viewForHeaderInSection: 0)

【讨论】:

以上是关于headerViewForSection:如果 Header View 或 section 在 iphone 上不可见,则返回 nil的主要内容,如果未能解决你的问题,请参考以下文章

UITapGesture在UITableview标题中

如果我不使用 MFC,是不是需要 afxres.h?如何从 .RC 脚本中删除它?

如果我只有一个 h_wnd,如何枚举 MFC 控件?

如果在全页刷新 (FPR) 后未呈现文件,我如何有条件地在 h:head 中呈现 .js 文件?

Codeforces 659G Fence Divercity dp

<ctype.h> - isalnum()