BUILD成功但输出不在iOS模拟器中

时间:2011-06-07 20:43:05

标签: iphone cocoa-touch uitableview

我想创建一个包含时区列表的简单表格视图。当我尝试“构建并运行”代码时,它表示“构建成功”,但没有表格视图,列表作为iOS模拟器输出。仅出现黑色空白屏幕。我无法弄清楚卡在哪里。所以请帮我找出解决方案。

代码如下。

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&lt;的UIKit / UIKit.h&GT;

@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

1 个答案:

答案 0 :(得分:0)

嗯,可能有几个原因导致这种情况发生。我要做的第一件事是在为rootViewController加载视图时检查数组是否为空。 如果它为空,则可能在加载视图后设置它,这就是您没有看到任何单元格的原因。您必须确保rootViewController的数组不为空。为此,您只需打印数组的计数并查看它有多少个对象。

否则,尝试添加[tableView reloadData];在rootViewController的viewDidLoad方法内或同一控制器的viewWillAppear方法中。