Ali*_*Ali 8 xcode objective-c storyboard uitableview xib
我想使用xib文件来自定义xcode(目标C)中的tableview部分,这里是我的文件:
SectionHeaderView.xib是一个带有UILabel的UIView
SectionHeaderView.m
#import "SectionHeaderView.h"
@implementation SectionHeaderView
@synthesize sectionHeader;
@end
Run Code Online (Sandbox Code Playgroud)
SectionHeaderView.h
#import <UIKit/UIKit.h>
@interface SectionHeaderView : UIView
{
IBOutlet UILabel *sectionHeader;
}
@property (nonatomic, strong) IBOutlet UILabel *sectionHeader;
@end
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,直到这里,但是只要我将XIB文件所有者的自定义类设置为"SectionHeaderView"并将Label连接到"sectionHeader",我将收到错误"NSUnknownKeyException".我想连接这些,所以我可以在返回haeder之前通过以下代码更改label.text:
header.sectionHeader.text = headerText;
Run Code Online (Sandbox Code Playgroud)
我正在为MasterViewController使用storyboard(xcode 4.5).非常感谢任何帮助
and*_*ani 15
您可以使用关联的xib创建UITableViewCell子类,并将其用作节头.在这个例子中,我将它称为CustomTableViewHeaderCell .h/.m/.xib,并向您展示如何更改此单元格内标签的文本.
在CustomTableViewHeaderCell.h中创建一个outlet属性
@property(弱,非原子)IBOutlet UILabel*sectionHeaderLabel;
将UITableViewCell添加到空的CustomTableViewHeaderCell.xib中,并 从Identity Inspector 中将该元素的类设置为CustomTableViewHeaderCell.
还设置标识符(单元格的属性检查器),例如 CustomIdentifier.
将标签拖到内容视图中并连接CustomTableViewHeaderCell (不是文件所有者!)的插座 .
然后在每个ViewController中,您要使用表视图部分标题单元格:
1)注册你的xib以重用标识符(可能在viewDidLoad中):
[_yourTableView registerNib:[UINib nibWithNibName:@"CustomTableViewHeader" bundle:nil] forCellReuseIdentifier:@"CustomIdentifier"];
Run Code Online (Sandbox Code Playgroud)
2)覆盖viewForHeaderInSection以显示自定义单元格标题视图
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
CustomTableViewHeaderCell * customHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"];
customHeaderCell.sectionHeaderLabel = @"What you want";
return customHeaderCell;
}
Run Code Online (Sandbox Code Playgroud)
试试这个:我在我的应用程序及其工作中测试了它:
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;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16738 次 |
最近记录: |