如何以编程方式创建UIView - > UIViewController - > UITableView

use*_*096 1 iphone objective-c ios4

我一直在努力在我的UITableViewController上面添加一个UIView.通过搜索,阅读和试验,我已经确定我应该只使用UIViewController而不是UITableViewController.由于各种原因,我很难完成这项工作,我想从更好的架构开始.

基本上我正在寻找可以帮助我完全以编程方式创建以下内容的示例代码/教程(没有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;

}
Run Code Online (Sandbox Code Playgroud)

要清楚,代码工作正常,问题是表的单元格行正在"渗透"插入此行的UIView微调器: [self.view insertSubview:myProgView aboveSubview:self.tableView]; 让我相信myProgView不是aboveSubview:self.tableView.

Bri*_*ian 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;
}
Run Code Online (Sandbox Code Playgroud)

您的视图控制器应该继承UITableViewController或实现UITableViewDataSourceUITableViewDelegate协议.