2012-06-23 45 views
0

我正在尝试为UITableView节头创建一个自定义UIView(.h .m & .xib)。无法识别的选择器 - 自定义UITableView节头

的目的是,我可以使用下面的行通过标题标题:

sectionHeaderView.sectionLabel.text = @"SOME TEXT"; 

然而,这总是会引起以下错误:

[UIView sectionLabel]: unrecognized selector sent to instance 0x6d3f6f0 

它为什么会认为这是一个UIView时我声明它为DUSettingsSectionView?这是代码。希望有人能指出我正确的方向。

==== ==== CODE

从我的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; 
} 

从DUSettingsSectionView.h

#import <UIKit/UIKit.h> 
@interface DUSettingsSectionView : UIView { 
    UILabel *sectionLabel; 
} 
@property (nonatomic, strong) IBOutlet UILabel *sectionLabel; 
@end 

从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已满y连接到.xib中,并将.xib设置为DUSettingsSectionView类。

+0

您展示在注释掉的分配你的'viewForHeaderInSection:'方法...是导致错误的那个吗? –

+0

是的,如果我取消注释赋值行然后我收到以下错误: - [UIView sectionLabel]:无法识别的选择器发送到实例0x6d3f6f0 2012-06-23 15:36:13.891 du [4108:207] ***终止应用程序由于未捕获异常'NSInvalidArgumentException',原因:' - [UIView sectionLabel]:无法识别的选择器发送到实例0x6d3f6f0' – Richard

+0

尝试'initWithFrame:'中的NSLog(@“Nib:%@”,nibArray);'它认为正在加载。 –

回答

0

看来你加载的.xib内DUSettingsSectionView的initWithFrame:方法,但你打电话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; 
} 
+0

还是用initWithFrame后同样的问题: 笔尖:( “的UIView <:0x6d54d40;帧=(0 0; 320 16);自动调整大小= W + H;层= >” ) 2012 -06-23 16:16:15.801 du [4545:207] - [UIView sectionLabel]:无法识别的选择器发送到实例0x6d54d40 2012-06-23 16:16:15.802 du [4545:207] ***终止应用程序到期未捕获的异常'NSInvalidArgumentException',原因:' - [UIView sectionLabel]:无法识别的选择发送到实例0x6d54d40' – Richard

+0

修复: 真的很抱歉家伙。问题纯粹是由于Interface Builder。我已经建立了与“文件所有者”的关系。一旦我删除了这些,并通过视图重新建立了连接,所有工作。然后NIB正确加载。感谢有用的指针。 – Richard

相关问题