Using an XIB File for a Custom Tableview Header Header

I wanted to use the xib file to configure the tableview section in xcode (target C), and here ar my files:

SectionHeaderView.xib is a UIView with UILabel

SectionHeaderView.m

#import "SectionHeaderView.h"

@implementation SectionHeaderView

@synthesize sectionHeader;

@end

SectionHeaderView.h

#import <UIKit/UIKit.h>

@interface SectionHeaderView : UIView
{
IBOutlet UILabel *sectionHeader;
}

@property (nonatomic, strong) IBOutlet UILabel *sectionHeader;

@end

and in my MasterViewController.m

#import "SectionHeaderView.h"

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

SectionHeaderView  *header = [[[NSBundle mainBundle] loadNibNamed:@"SectionHeaderView" owner:self options:nil] objectAtIndex:0];

return header;

}

It has been working fine so far, however, as soon as I set my own class of the XIB file owner to "SectionHeaderView" and connected the shortcut to "sectionHeader", I will get the error "NSUnknownKeyException". I wanted to link them so that I could change label.text with the following code before returning haeder:

header.sectionHeader.text = headerText;

I am using a storyboard (xcode 4.5) for MasterViewController. Would thank for any help

+7
3

: :

NSArray *viewArray =  [[NSBundle mainBundle] loadNibNamed:@"SectionHeaderview" owner:self options:nil];  
UIView *view = [viewArray objectAtIndex:0]; 
UILabel *lblTitle = [view viewWithTag:101]; 
lblTitle.text = @"Text you want to set"; 
return view;
+9

UITableViewCell xib . CustomTableViewHeaderCell.h/.m/.xib , .

  • outlet CustomTableViewHeaderCell.h

    @property (, ) IBOutlet UILabel * sectionHeaderLabel;

  • UITableViewCell CustomTableViewHeaderCell.xib CustomTableViewHeaderCell .

  • ( ), CustomIdentifier.

  • CustomTableViewHeaderCell ( !).

ViewController :

1) xib (, viewDidLoad):

[_yourTableView registerNib:[UINib nibWithNibName:@"CustomTableViewHeader" bundle:nil] forCellReuseIdentifier:@"CustomIdentifier"];

2) viewForHeaderInSection

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CustomTableViewHeaderCell * customHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"];
    customHeaderCell.sectionHeaderLabel = @"What you want";
    return customHeaderCell;
}
+13

:

1) HeaderView UIView. UIViewController. .

2) IBOutlet UILabel (, 101).

SectionHeaderview.

sectionHeaderView.XIB, .m .h.

: Viewforheader MasterViewController:

{
    UIViewController *vc=[[UIViewController alloc] initWithNibName:@"SectionHeaderview" bundle:nil]

    UILable *lblTitle =[vc.view viewWithTag:101];

    lblTitle.text =@"Text you want to set";

    return vc.view;
}
+2
source

All Articles