2016-04-12 7 views
0

私はiOSプログラミングには新しく、BIG NERD RANCH IOS PROGRAMMING 4TH EDITIONの本ではなく、NIBの代わりにストーリーボードを使ってUITableViewControllerを構築する例に従いました。UITableViewのヘッダとしてUIViewを初期化する方法

ヘッダーに追加したUIViewが表示されない点を除いて、すべてが機能します。

私は、UIViewをストーリーボードのプロトタイプセルの上にUIViewTableControllerに追加しました。 UIViewには、私は2つのボタンを持っていた。初期ビューコントローラとしてUITableViewControllerを設定すると、ストーリーボードは私がストーリーボードで設計したように表示されます。しかし、AppDelegate.mを使用すると、行を追加する

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    TableTest *table = [[TableTest alloc] init]; 
    self.window.rootViewController = table; 
    return YES; 
} 

UIViewは表示されません。私は、だから私は私の限られた知識と少し検索してみました...

をheaderViewとしてUIViewのための出口を割り当てられ、私はUIViewのが初期化されていないと仮定それは空白の灰色のヘッダを示し

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 

    UIView *header = self.headerView; 
    return header; 

} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 

    return 40; 

} 

を試してみましたインターネット上

(UIView *) headerView{ 
    if(!_headerView){ 
      _headerView = [_headerView init] 
    } 
    return _headerView 
} 

を追加することにより、まだ私はAppDelegateを使用するときに表示されますので、私はそれを初期化する必要がありますどのように

を動作していませんか?

+0

を動作

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]; return [storyboard instantiateViewControllerWithIdentifier:@"TableTest"]; 

によりinitメソッドを書き直し。 try _headerView = [UIView new]; – larva

+1

質問はtableviewセクションのヘッダーであり、tableviewヘッダーではありません。 – Joshua

答えて

0

は、それは私がストーリーボードを初期化するのを忘れ判明viewDidLoadメソッド

self.tableView.tableHeaderView = ({ 
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 183.0f)]; 
     view.backgroundColor=[UIColor colorWithRed:0.376 green:0.086 blue:0.09 alpha:1]; 
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 150, 0, 24)]; 
     label.text = @"ffff"; 
     label.font = [UIFont fontWithName:@"HelveticaNeue" size:21]; 
     label.backgroundColor = [UIColor clearColor]; 
     label.textColor = [UIColor whiteColor]; 
     [label sizeToFit]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin; 


     [view addSubview:label]; 
     view; 
    }); 
0

にこのコードを置きます。まあそれはちょっとばかげている。 Iは、ストーリーボードのIDが割り当てられ、それが_headerView = [_headerView INIT]であるもの

関連する問題