繁体   English   中英

根据数组大小更改self.view

[英]Changing self.view depending on size of array

我有一个具有UITabBarController的应用程序。 我要实现的是,如果array属性中有项目(从CoreData加载),则TabBar中包含的第一个ViewController将显示TableView,否则将显示UIImageView (包含有关如何添加项目的更多信息)。

如果用户转到另一个TabBarItem,然后回到第一个TabBarItem,则可能已经填充了数组。 因此,它应该更改显示的内容。 有人可以发布一个程序段来实现此目的。 我尝试了几件事,但都没有正常工作。

[使用对象:无,自行selfselectSelectorInBackground:@selector(loadRecentInBackground)];

然后定义选择器

  - (void) loadRecentInBackground{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 

  // Define put the values into the data structure that you retrieve 
  // (I use an NSMutableDictionary)

  [pool release]; 

}

并从字典中检索值

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

并确保添加以下内容:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

返回数据结构的计数。

最简单的方法是根据是否填充数组来设置标志。 然后调整表格以显示数据行或显示带有指令的UIImageView的单行。

最好在-viewWillDisplay设置标志并通过基于– tableView:heightForRowAtIndexPath:– tableView:willDisplayCell:forRowAtIndexPath:– numberOfSectionsInTableView:的标志返回正确的值来设置表的格式。

此代码可以完成工作

- (void)loadView {

    UIView* aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];

    //tableView
    self.tableView = [[[UITableView alloc ] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain] autorelease];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);                    

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.autoresizesSubviews = YES;

    [aView addSubview: tableView];

    self.view   = aView;
    [aView release];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [self loadData];

    //Display or remove Image depending on the ammount of results.
    if ([self.itemsArray count] == 0) {
        NSLog(@"No Items in Array");

        if (noDataImageView == nil) {
            noDataImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"NoDataBackground.png"]];
            [self.view addSubview:noDataImageView];
        }

    }else{
        if(noDataImageView != nil){
            [noDataImageView removeFromSuperview];
        }
    }

}

- (void)dealloc {
    [super dealloc];
    [tableView release];
    [noDataImageView release];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM