2011-01-13 60 views
1

我一直在努力在UITableViewController上添加一個UIView。通過搜索,閱讀和試驗我已經確定,而不是一個UITableViewController,我應該只是使用UIViewController。由於各種各樣的原因,我很難做這項工作,我想用更好的體系結構重新開始。如何以編程方式創建UIView - > UIViewController - > UITableView

基本上我正在尋找的代碼示例/教程,可以幫助我創建完全編程以下(無NIBS):

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

爲什麼我想我上面的UITableView一個UIView是我想成爲的原因能夠在我的桌子上面添加UIViews。

-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微調器「流血」: [self.view insertSubview:myProgView aboveSubview:self.tableView]; 使我相信myProgView不是aboveSubview:self.tableView

+0

爲什麼你想避免筆尖? – kubi 2011-01-13 18:22:34

回答

6

視圖和控制器是分開的東西。您可以擁有視圖控制器層次結構和視圖層次結構。但是他們沒有交錯,因爲帖子的標題暗示(我知道,Interface Builder將它們顯示爲單個層次結構,但視圖和控制器更像是兩個並行層次結構)。

無論如何,你可以很容易地讓視圖控制器在代碼中設置任何視圖。覆蓋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 2011-01-14 17:19:56

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.