BUILD is succeeded but the output is not coming in iOS Simulator
I want to create a simple table view with list of time zones. When I try to "Build and Run" the code, it said that "Build succeeded", but there is no table view with the list as iOS simulator output. Only a black blank screen appears. I could not be able to figure out where I got stuck. So please help me to find out the solution.
The codes are as follows.
1. RootViewController.h
#import < UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSArray *timeZoneNames;
}
@property (nonatomic, retain) NSArray *timeZoneNames;
@end
2. RootViewController.m
#import "RootViewController.h"
#import "SimpleTableViewAppDelegate.h"
@implementation RootViewController
@synthesize timeZoneNames;
- (void)viewDidLoad {
self.title = NSLocalizedString(@"Time Zones", @"Time Zones Title");
}
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [timeZoneNames count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)
indexPath {
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Configure the cell.
NSString *timeZoneName = [timeZoneNames objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneName;
return cell;
}
//The Table view has only one section
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)
indexPath {
return nil;
}
- (void)dealloc {
[timeZoneNames release];
[super dealloc];
}
@end
3 SimpleTableViewAppDelegate.h
import < UIKit/UIKit.h>
@interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
SimpleTableViewAppDelegate.m
#import "SimpleTableViewAppDelegate.h"
#import "RootViewController.h"
@implementation SimpleTableViewAppDelegate
@synthesize window;
@synthesize navigationController;
- (void)aplicationDidFinishLaunching:(UIApplication *)application {
开发者_如何学运维
RootViewController *rootViewController = [[RootViewController alloc]
initWithStyle:UITableViewStylePlain];
//Retrieve the array of known time zone names, then sort the array and pass it to the root
//view controller.
NSArray *timeZones = [NSTimeZone knownTimeZoneNames];
rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.navigationController = aNavigationController;
[aNavigationController release];
[rootViewController release];
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
-(void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}
@end
Hmmm, there can be a couple of reasons of why this is happening. The first thing that I would do is check if the array is empty when the view is loaded for the rootViewController. If it is empty, it is probably being set after the view has been loaded and that is the reason why you don't see any cells. You have to make sure that the array is not empty for the rootViewController. For this you can simply print the count for the array and see how many objects it has.
Else, try adding a [tableView reloadData]; inside either the viewDidLoad method for your rootViewController or in the viewWillAppear method in the same controller.
精彩评论