2012-09-23 35 views
6

一個XIB文件,我想用一個XIB文件來定製Xcode的實現代碼如下部分(客觀C),這裏AR我的文件:使用自定義泰伯維節頭

SectionHeaderView.xib是一個UILabel一個UIView

SectionHeaderView.m

#import "SectionHeaderView.h" 

@implementation SectionHeaderView 

@synthesize sectionHeader; 

@end 

SectionHeaderView.h

#import <UIKit/UIKit.h> 

@interface SectionHeaderView : UIView 
{ 
IBOutlet UILabel *sectionHeader; 
} 

@property (nonatomic, strong) IBOutlet UILabel *sectionHeader; 

@end 

在我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:

header.sectionHeader.text = headerText; 

我使用的MasterViewController故事板(Xcode的4.5)。 希望得到任何幫助

回答

8

試試這個:

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; 
+0

謝謝,這是工作:) – Ali

2

您可以通過以下途徑之一解決此問題:

1)你已經派生SectionHeaderView從UIView。改爲使用UIViewController來派生這個類。這將解決您的問題。

2)代替使用IBOutlet屬性,在視圖中設置標記UILabel(如101)。

放棄SectionHeaderview類。

保留SectionHeaderView.XIB,僅刪除.m和.h文件。

使用下面的代碼INS MasterViewController類的Viewforheader方法:

{ 
    UIViewController *vc=[[UIViewController alloc] initWithNibName:@"SectionHeaderview" bundle:nil] 

    UILable *lblTitle =[vc.view viewWithTag:101]; 

    lblTitle.text [email protected]"Text you want to set"; 

    return vc.view; 
} 
+0

我試過第二種方式,並得到一個錯誤:' - [UIViewCo ntroller _loadViewFromNibNamed:bundle:]加載了「SectionFooterView」筆尖,但未設置視圖插口。' – Ali

+0

試試這個:我測試了它在我的應用程序和它的工作:'code'NSArray * arrviw = [[NSBundle mainBundle] loadNibNamed:@「SectionHeaderview」owner:self options:nil]; UIView * view = [arrviw objectAtIndex:0]; UILable * lblTitle = [view viewWithTag:101]; lblTitle.text = @「您要設置的文字」;返回視圖; – user1113101

+0

將UIViewController設置爲另一個UIViewController的標頭的優點是什麼?頭文件實際上是一個UIView。原來的問題是如何將nib文件作爲頭文件加載。 –

13

您可以創建一個UITableViewCell子類有:我在我的應用程序和工作進行了測試關聯的xib,並將其用作部分標題。在這個例子中,我將它稱爲CustomTableViewHeaderCell .h/.m/.xib,並向您展示如何更改此單元格內的標籤文本。

  • 在CustomTableViewHeaderCell.h創建出口屬性

    @property(弱,非原子)IBOutlet中的UILabel * sectionHeaderLabel;

  • 添加一個UITableViewCell到空CustomTableViewHeaderCell.xib 和類元素的從身份檢查設置爲CustomTableViewHeaderCell

  • 還設置標識符(單元的屬性檢查器)例如 定製標識符

  • 拖動標籤到內容的瀏覽,並從 CustomTableViewHeaderCell連接插座(不是文件所有者!)。

然後在每個視圖控制器,你要使用表格視圖節頭單元:

1)註冊您的廈門國際銀行重用標識符(可能是在viewDidLoad中):

[_yourTableView registerNib:[UINib nibWithNibName:@"CustomTableViewHeader" bundle:nil] forCellReuseIdentifier:@"CustomIdentifier"]; 

2)覆蓋viewForHeaderInSection顯示您的自定義單元格標題視圖

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    CustomTableViewHeaderCell * customHeaderCell = [tableView dequeueReusableCellWithIdentifier:@"CustomIdentifier"]; 
    customHeaderCell.sectionHeaderLabel = @"What you want"; 
    return customHeaderCell; 
}