I'm trying to create a custom UIView (.h .m & .xib) for a UITableView Section Header.
The aim is that I can pass the Heading title using the following line:
sectionHeaderView.sectionLabel.text = @"SOME TEXT";
However this always causes the following error:
[UIView sectionLabel]: unrecognized selector sent to instance 0x6d3f6f0
Why does it think this is a UIView when I declare it as DUSettingsSectionView? Here is the code. Hopefully someone can point me in the right direction.
==== CODE ====
From my UIViewController
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 16;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
DUSettingsSectionView *sectionHeaderView = [[DUSettingsSectionView alloc] init];
// sectionHeaderView.sectionLabel.text = @"SOME TEXT";
return sectionHeaderView;
}
From DUSettingsSectionView.h
#import <UIKit/UIKit.h>
@interface DUSettingsSectionView : UIView {
UILabel *sectionLabel;
}
@property (nonatomic, strong) IBOutlet UILabel *sectionLabel;
@end
From DUSettingsSectionView.m
#import "DUSettingsSectionView.h"
@implementation DUSettingsSectionView
@synthesize sectionLabel;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"DUSettingsSectionView" owner:self options:nil];
self = [nibArray objectAtIndex:0];
}
return self;
}
@end
sectionLabel is fully connected inside the .xib and the .xib is set to the DUSettingsSectionView class.