I am making use of a UIButton that I added to header view in checking the numberOfRowsInSection: method and on the button state I am checking if there are rows present in that section or not. But my app crashes when the [self.tableView headerViewForSection:section] returns nil. Since it is working fine for sections that are visible, I am assuming this is happening because my section is not visible at this moment.
Here is how I create my section header view:
- (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;
}
and here is how I populate data in UITableView in 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;
}
Since the button is returned nil here, while before updating the rowCount was 1 for section 5, it throws an exception. What should be done so that
[self.tableView headerViewForSection:section]
DOESN'T return nil for invisible section?