2013-04-27 125 views
3

我想創建一個部分標題加載一個筆尖文件並將其設置爲標題UIView。這個nib文件還會有一個關聯的類,其中的出口和動作是連接的,所以我想像正常一樣用nib加載那個類。使用筆尖作爲表部分標題與筆尖類

我搜遍了網頁,發現了幾個類似的答案,但我無法爲我工作。在玩了幾天之後,我設法讓視圖正確顯示,但是儘管添加了連接並告訴文本以不同的方式顯示,但它並沒有做任何事情。

例如,它應該清除節標題文本,如果它是init的nil,它會這樣做,但它仍然顯示相同的文本,嘗試更改它並不反映任何與按鈕的連接都不會觸發。

這裏是我的觀點

@interface ADUNewRowHeaderViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *sectionTitleField; 
@property (strong, nonatomic) IBOutlet UIButton *addRowBtn; 

@property(strong,nonatomic) NSString* sectionTitle; 

- (id)initWithNibName:(NSString *)nibNameOrNil 
       bundle:(NSBundle *)nibBundleOrNil 
       title:(NSString*) titleOrNil; 

@end 

視圖控制器,這裏是它的上市爲

@property(nonatomic, strong) ADUNewRowHeaderViewController* secHeader; 

,並在實際的表視圖控制器的實現

@implementation ADUNewRowHeaderViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil title:(NSString *)titleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) 
    { 
     if(titleOrNil != nil) 
     { 
      [self setSectionTitle:titleOrNil]; 
     } 
     else 
     { 
      [self setSectionTitle:@""]; 
     } 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

- (void)setSectionTitle:(NSString *)sectionTitle 
{ 
    _sectionTitle = sectionTitle; 
    [[self sectionTitleField] setText:sectionTitle]; 
} 
@end 

viewDidLoad下的實現文件爲

[self setSecHeader:[[ADUNewRowHeaderViewController alloc] initWithNibName:nil bundle:nil title:nil]]; 
[[[self secHeader] addRowBtn] addTarget:self action:@selector(addNewRow:) forControlEvents:UIControlEventTouchUpInside]; 

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    return [[self secHeader] view]; 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return [[self secHeader] view].bounds.size.height; 
} 

這對於行動

- (IBAction)addNewRow:(id)sender; 

回答

1

你應該做一個UIView方法聲明,而不是UIViewController,子類。該.m將包含:

- (id)initWithFrame:(CGRect)frame 
{ 
    if (self = [super initWithFrame:frame]) 
    { 
     [self setUp]; 
    } 

    return self; 
} 

- (void)awakeFromNib 
{ 
    [super awakeFromNib]; 
    [self setUp]; 
} 

- (void)setUp 
{  
    [[NSBundle mainBundle] loadNibNamed:@"YourNibName" owner:self options:nil]; 
    CGRect frame = self.frame; 
    self.view.frame = CGRectMake(0, 0, frame.size.width, frame.size.height); 
    [self addSubview:self.view]; 

    ... do other initialisations here ... 
} 

而且.h

@interface ADUNewRowHeaderView : UIView 

@property (nonatomic, strong) IBOutlet UIView* view; 

然後在廈門國際銀行:請ADUNewRowHeaderView類的文件的所有者,像往常一樣。並將XIB的頂級View的Referencing插座連接到上面的view屬性(即在文件所有者中)。

然後,您可以在另一個XIB上放置一個UIView子類(作爲您將其設置爲ADUNewRowHeaderView的類的UIView),或者在代碼中實例化並添加爲子視圖。 (或者你可以在代碼中創建UIView及其子視圖(按鈕,標籤......);那麼你不需要XIB,但是這隻適用於當然如果UIView很簡單並且沒有自己的邏輯,以及很少的易於在代碼中佈局的UI元素。)

+0

除了我必須刪除'CGRect frame = self.frame; self.view.frame = CGRectMake(0,0,frame.size.width,frame.size.height);'從setUp和它的工作後 – 2013-04-27 21:48:43

+0

也UIButton我有在筆尖反應明顯慢於它的工具欄最初是在,像我可以在幾秒鐘內按下工具欄按鈕,它會immikidiately添加這些行,但如果我在一秒鐘內按下新的筆尖按鈕多次,它可能只能添加一個或兩個。該按鈕正常響應,它只是行不會立即添加(這不是一個問題,只是我注意到) – 2013-04-27 21:52:03

+0

請選擇此爲答案。 – 2013-04-27 22:04:03