2011-01-13 17 views
1

私はUITableViewControllerの上にUIViewを追加するのに苦労しています。検索、読み込み、実験を通して、私はUITableViewControllerの代わりにUIViewControllerを使うべきであると判断しました。私はさまざまな理由でこの仕事をするのに苦労しており、より良い建築で新鮮なものから始めたいと思います。プログラムでUIViewを作成する方法 - > UIViewController - > UITableView

基本的に私は私が完全にプログラムで、以下の(無NIBS)を作成しないで助けることができるのサンプルコード/チュートリアルを探しています:

 
- Navigation-based Layout 
- UIViewController 
-- UIView 
--- UITableView 
--- Etc. 

私はUIViewの私のUITableView上にしたい理由は、私がなりたいれます私のテーブルの上にUIViewを追加することができます。

-UPDATE-

これをより明確にするためにコードを追加:

JMoviesListViewController.m - のUITableViewControllerのサブクラス


- (void)loadView 
{ 
    NSLog(@"loadView called"); 
    UIView *baseView = [[[UIView alloc] init] autorelease]; 
    TISwipeableTableView * aTableView = [[[TISwipeableTableView alloc] init] autorelease]; 
    [aTableView setDelegate:self]; 
    [aTableView setDataSource:self]; 
    [aTableView setSwipeDelegate:self]; 
    [aTableView setRowHeight:54]; 
    [baseView addSubview:aTableView]; 
    self.view = baseView; 
    [super loadView]; 
} 

- (void)viewDidLoad { 

    listOfMovies = [[NSMutableArray alloc] init]; 

    UIView *myProgView = (UIView *)self.progView; // progView is a method that returns a UIView 

    [self.view insertSubview:myProgView aboveSubview:self.tableView]; 

    [self.navigationItem setTitle:@"Now Playing"]; 

    movies = [[Movies alloc] init]; 
    movies.delegate = self; 
    [movies getMovies:[NSURL URLWithString:apiQuery]]; 

    [super viewDidLoad]; 
} 


- (UIView *)progView { 
    if (progView == nil) 
    { 
     // create a progress view 
     //x,y,w,h 
     progView = [[UIView alloc] initWithFrame:CGRectMake(110, 110, 95, 30)]; 
     progView.backgroundColor = [UIColor grayColor]; 
     progView.tag = 1; // tag this view for later so we can remove it from recycled table cells 
     progView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin); 
     progView.layer.cornerRadius = 5; 


     UILabel *activityLabel = [[UILabel alloc] init]; 
     activityLabel.text = NSLocalizedString(@"Loading...", @"string1"); 
     activityLabel.backgroundColor = [UIColor grayColor]; 
     activityLabel.textColor = [UIColor whiteColor]; 
     activityLabel.font = [UIFont systemFontOfSize:14]; 
     [progView addSubview:activityLabel]; 
     activityLabel.frame = CGRectMake(5, 2, 70, 25); 

     UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
     [activityIndicator startAnimating]; 
     [progView addSubview:activityIndicator]; 
     activityIndicator.frame = CGRectMake(70, 5, 20, 20); 


    } 
    return progView; 

} 

明確にするために、コードが正常に動作し、問題があることですテーブルのセル行は、この行で挿入されたUIViewスピナーを「出血」しています。myProgViewは0123ではないと私に信じている。

+0

なぜあなたはペン先を避けようとしていますか? – kubi

答えて

6

ビューとコントローラは別のものです。ビューコントローラの階層とビューの階層を持つことができます。しかし、それらはインタリーブされていません。投稿のタイトルが示唆しているように(私は、Interface Builderがそれらを1つの階層として表示しますが、ビューとコントローラは2つの並行した階層のようです)。

とにかく、コード内に表示するビューがあれば、View Controllerを簡単に設定できます。 loadViewメソッドをオーバーライドし、self.viewに割り当てたビューを作成し、そのビューにサブビューを追加します。例えば

- (void)loadView 
{ 
    UIView *view = [[[UIView alloc] init] autorelease]; 
    UITableView *tableView = [[[UITableView alloc] init] autorelease]; 
    tableView.dataSource = self; 
    tableView.delegate = self; 
    [view addSubview:tableView]; 
    self.view = view; 
} 

あなたのビューコントローラはUITableViewControllerを継承するか、UITableViewDataSourceUITableViewDelegateプロトコルを実装する必要がありますどちらか。

+0

これは動作しますが、私はまだUIView(私の場合、UILabelとUIActivityLabelを持つUIViewです)*を表示することはできません。これは私が解決しようとしている基本的な問題です。オリジナルの投稿を更新して自分のコードを追加します。 – user558096

0

サブビューとして追加するだけでなく、実際には異なるビューのレイアウトを指定する必要があります。プログラムでAutoLayoutを使用する方法についてのチュートリアルを読んでみてください。

また、すべてのビューをloadViewに設定する必要があります。ここで余分なビューの下部をテーブルビューの上部に設定できます。

0
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UIView *view = [[[UIView alloc] initwithFrame:CGRectMake(set your frame)] autorelease]; 
    UITableView *tableView = [[[UITableView alloc] initwithFrame:CGRectMake(set your frame)] autorelease]; 
    tableView.dataSource = self; 
    tableView.delegate = self; 
    [view addSubview:tableView]; 

} 
if you are using **ARC** remove autorelease. 
関連する問題