0

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.

4

1 回答 1

0

似乎您加载了initWithFrame:DUSettingsSectionView 的 .xib 内部方法,但您init在内部调用(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 要修复它,请更改(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section为以下内容:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  DUSettingsSectionView *sectionHeaderView = [[DUSettingsSectionView alloc] initWithFrame:frame];
  sectionHeaderView.sectionLabel.text = @"SOME TEXT";
  return sectionHeaderView;
}
于 2012-06-23T12:06:44.150 回答