【问题标题】:How to programmatically create UIView -> UIViewController -> UITableView如何以编程方式创建 UIView -> UIViewController -> UITableView
【发布时间】:2011-06-08 16:26:36
【问题描述】:

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

基本上,我正在寻找可以帮助我完全以编程方式(无 NIBS)创建以下内容的示例代码/教程:

- 基于导航的布局 - UIViewController -- 界面视图 --- UITableView - - 等等。

我想要在 UITableView 上方添加 UIView 的原因是我希望能够在表格上方添加 UIView。

-更新-

添加代码以使其更清晰:

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

【问题讨论】:

  • 你为什么要避免使用笔尖?

标签: iphone objective-c ios4


【解决方案1】:

视图和控制器是分开的。您可以拥有视图控制器的层次结构和视图的层次结构。但是它们并没有交错,正如文章标题所暗示的那样(我知道,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 协议。

【讨论】:

  • 这行得通,但我仍然无法在 表上显示 UIView(在我的情况下是带有 UILabel 和 UIActivityLabel 的 UIView)。这是我要解决的基本问题。我将更新我的原始帖子以包含我的代码。
【解决方案2】:

您必须实际指定不同视图的布局,而不仅仅是将它们添加为子视图。尝试阅读有关以编程方式使用 AutoLayout 的教程。

您还想在 loadView 中设置所有视图。在那里,您可以将额外视图的底部设置为表格视图的顶部。

【讨论】:

    【解决方案3】:
    - (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.
    

    【讨论】:

      猜你喜欢
      • 2011-02-10
      • 1970-01-01
      • 2018-06-04
      • 2013-03-28
      • 1970-01-01
      • 2021-02-14
      • 2011-06-17
      • 1970-01-01
      • 2012-06-18
      相关资源
      最近更新 更多