Objective c 我想通过uitableview单元情节提要将xib文件使用到
注:Objective c 我想通过uitableview单元情节提要将xib文件使用到,objective-c,Objective C,注: Category是我的模型类,它还包含我的网络服务 SectionHeaderView是我的xib视图,其中包含UILabel名为:categoryLabel 我想将数据显示在xib视图的UILabel字段中,并显示在UITableviewCell的header部分 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { SectionHeaderView
Category
是我的模型类,它还包含我的网络服务
SectionHeaderView
是我的xib视图,其中包含UILabel
名为:categoryLabel
我想将数据显示在xib视图的UILabel字段中,并显示在UITableviewCell的header部分
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SectionHeaderView *sectionHeaderView = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
Category *sectionInfo = (self.sectionInfoArray)[section];
sectionHeaderView.categoryLabel.text = sectionInfo.categoryName;
NSLog(@"header section: %@", sectionInfo.categoryName);
sectionHeaderView.section = section;
sectionHeaderView.delegate = self;
return sectionHeaderView;
}
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合密钥视图的密钥值编码。”
***第一次抛出调用堆栈:
试试这段代码
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NSObject 0x14ee9d50> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key view.'
*** First throw call stack:
您没有在自定义
部分HeaderView
中提供足够的关于如何初始化header视图的信息,无论如何,我将给出一个示例,说明如何使用自定义视图和xib文件装箱自定义header视图
首先,您需要对UITableViewHeaderFooterView
进行子类化,将其命名为SectionHeaderView
在xib中,确保将UIView
类设置为SectionHeaderView
。那么只有你才能为UI元素创建出口
在部分headerView.h
中
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
SectionHeaderView *sectionHeaderView = (SectionHeaderView *) [[[NSBundle mainBundle] loadNibNamed: @"SectionHeaderView" owner:self options:nil] firstObject];
Category *sectionInfo = (self.sectionInfoArray)[section];
sectionHeaderView.categoryLabel.text = sectionInfo.categoryName;
NSLog(@"header section: %@", sectionInfo.categoryName);
sectionHeaderView.section = section;
sectionHeaderView.delegate = self;
return sectionHeaderView;
}
在部分headerview.m
@interface SectionHeaderView : UITableViewHeaderFooterView
@property (weak, nonatomic) IBOutlet UILabel *categoryLabel;
@end
在ViewController.m
中,只需注册您将要使用的xib文件
#import "SectionHeaderView.h"
@implementation SectionHeaderView
- (void)awakeFromNib
{
//do things after nib is loaded
[super awakeFromNib];
}
- (void)layoutSubviews
{
[super layoutSubviews];
//set frames hear
}
@end
在方法上,
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.aTableView registerNib:[UINib nibWithNibName:@"SectionHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"HEADER_REUSE_ID"];
}
您是否在Xib中将各种对象(重用视图)的正确类设置为SectionHeaderView?是的,我可以将我的categoryName数据显示为uitableview单元格的textlabel,但如果我希望在uilabel连接到uitableviewcell类的情况下显示相同的数据,则会出现上述错误。这是因为我直接显示模型类中的categoryname。
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
SectionHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"HEADER_REUSE_ID"];
view.categoryLabel.text = [NSString stringWithFormat:@"section:%ld",(long)section];
return view;
}