2015-02-03 2 views
0

numberOfRowsInSection : 메서드를 검사 할 때 헤더보기에 추가 한 UIButton을 사용하고 있으며 현재 행이 있는지 확인하고 있습니다. 그 섹션에서 또는 아닙니다. 그러나 [self.tableView headerViewForSection : section]이 nil을 반환하면 내 앱이 다운됩니다. 보이는 섹션에 잘 작동하기 때문에 내 섹션이 현재 표시되지 않기 때문에이 상황이 발생한다고 가정합니다. 여기headerViewForSection : 머리글보기 또는 섹션이 아이폰에 표시되지 않으면 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에서 jQuery과 데이터를 채우는 방법은 다음과 같습니다

버튼은 이전하면서, 여기에 nil을 반환
- (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; 
} 

때문에 섹션 5에 대해 rowCount를 1로 업데이트하면 예외가 발생합니다. 그렇게해야 할 일

[self.tableView headerViewForSection:section] 

보이지 않는 섹션에 대해 nil을 반환하지 않습니까?

답변

0

그것은 당신의 질문에 대한 대답은 아니지만, 아마 당신의 목적에 대한 해결 방법입니다 :
내가 sectionArray의 항목에 키 - 값 쌍을 추가합니다 (예 : "visible" = "true/false/0/1/..."). 섹션의 버튼 동작에서이 값을 수정하십시오. 그런 다음 numberOfRowsInSection... 메서드의 각 항목에 대한 값을 읽고 섹션이 현재 표시되는지 여부에 따라 0 또는 sectionArray.count을 반환합니다.
희망이 있습니다.

+0

고마워요! 이 질문에 대한 대답은 아니지만 문제의 적절한 해결 방법입니다. 나는 비슷한 것을 구현했다. 많은 감사합니다! –

관련 문제