Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a XIB file for custom Tableview Section Header

I wanted to use a xib file to customise a tableview section in xcode (objective C), and here ar my files:

SectionHeaderView.xib is a UIView with a 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 works ok till here, however as soon as I set XIB file owner's custom class to "SectionHeaderView" and connect the Label to "sectionHeader" I will get the error "NSUnknownKeyException". I wanted to connect these so I could change the label.text by the following code before returning the haeder:

header.sectionHeader.text = headerText;

I am using storyboard (xcode 4.5) for the MasterViewController. Would appreciate any help

like image 775
Ali Avatar asked Sep 23 '12 22:09

Ali


2 Answers

You can create a UITableViewCell subclass with an associated xib, and use it as the section header. In this example i will call it CustomTableViewHeaderCell.h/.m/.xib and show you how to change the text of a label inside this cell.

  • Create an outlet property in your CustomTableViewHeaderCell.h

    @property (weak, nonatomic) IBOutlet UILabel *sectionHeaderLabel;

  • Add a UITableViewCell into the empty CustomTableViewHeaderCell.xib and set the class of the element to CustomTableViewHeaderCell from the Identity Inspector.

  • Set also the Identifier (attribute inspector of the cell) for example CustomIdentifier.

  • Drag a label into the Content View and connect the outlet from the CustomTableViewHeaderCell (Not the file owner!).

Then in each ViewController you want to use the table view section header cell:

1) Register your xib to reuse identifier (probably in viewDidLoad):

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

2) Override viewForHeaderInSection to display your custom cell header view

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    CustomTableViewHeaderCell * customHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"];
    customHeaderCell.sectionHeaderLabel = @"What you want";
    return customHeaderCell;
}
like image 53
andreacipriani Avatar answered Sep 19 '22 18:09

andreacipriani


Try this: I have tested it in my app and its working:

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;
like image 21
user1113101 Avatar answered Sep 20 '22 18:09

user1113101