I have a an app that has a UITabBarController
.
What I would like to achieve, is that the first ViewController included in the TabBar displays a TableView if there are items in the array property (loaded from CoreData), or a UIImageView
(with more information ab开发者_StackOverflow社区out how to add items) if not.
If the user goes to a different TabBarItem, and comes back to the first TabBarItem the array could have been populated. So it should change what is displayed accordingly. Could somebody post a programatic snippet to achieve this. I have tried several things, and none have worked properly.
[self performSelectorInBackground:@selector(loadRecentInBackground) withObject:nil];
Then defining the selector
- (void) loadRecentInBackground{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Define put the values into the data structure that you retrieve
// (I use an NSMutableDictionary)
[pool release];
}
and retrieve the value from the dictionary in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
and make sure to add the:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
where you return the count of the data structure.
The easiest way would be to set a flag based on whether the array is populated or not. Then adjusting the table to display either rows of data or a single row displaying an UIImageView with the instructions.
It would be best to set the flag in -viewWillDisplay
and the format the table by returning the correct values based on the flag from – tableView:heightForRowAtIndexPath:
, – tableView:willDisplayCell:forRowAtIndexPath:
and – numberOfSectionsInTableView:
.
This code does the job
- (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];
}
精彩评论