我想创建一个包含时区列表的简单表格视图。当我尝试“构建并运行”代码时,它表示“构建成功”,但没有表格视图,列表作为iOS模拟器输出。仅出现黑色空白屏幕。我无法弄清楚卡在哪里。所以请帮我找出解决方案。
代码如下。
#import < UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSArray *timeZoneNames;
}
@property (nonatomic, retain) NSArray *timeZoneNames;
@end
#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
@interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@end
#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
答案 0 :(得分:0)
否则,尝试添加[tableView reloadData];在rootViewController的viewDidLoad方法内或同一控制器的viewWillAppear方法中。