使用 XIB 文件自定义 Tableview 部分标题 [英] Using a XIB file for custom Tableview Section Header
问题描述
我想使用一个 xib 文件来自定义 xcode 中的 tableview 部分(目标 C),这里是我的文件:
I wanted to use a xib file to customise a tableview section in xcode (objective C), and here ar my files:
SectionHeaderView.xib 是一个带有 UILabel 的 UIView
SectionHeaderView.xib is a UIView with a UILabel
SectionHeaderView.m
SectionHeaderView.m
#import "SectionHeaderView.h"
@implementation SectionHeaderView
@synthesize sectionHeader;
@end
SectionHeaderView.h
SectionHeaderView.h
#import <UIKit/UIKit.h>
@interface SectionHeaderView : UIView
{
IBOutlet UILabel *sectionHeader;
}
@property (nonatomic, strong) IBOutlet UILabel *sectionHeader;
@end
在我的 MasterViewController.m 中
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;
}
它在这里工作正常,但是一旦我将 XIB 文件所有者的自定义类设置为SectionHeaderView"并将标签连接到sectionHeader",我就会收到错误NSUnknownKeyException".我想连接这些,以便我可以在返回 haeder 之前通过以下代码更改 label.text:
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;
我正在为 MasterViewController 使用故事板 (xcode 4.5).将不胜感激任何帮助
I am using storyboard (xcode 4.5) for the MasterViewController. Would appreciate any help
推荐答案
试试这个:我已经在我的应用程序中测试了它并且它的工作原理:
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;
这篇关于使用 XIB 文件自定义 Tableview 部分标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!