创建表视图后如何解决IOS异常



创建第二个表视图控制器后,我在单击连接到此表视图的选项卡栏项时出现以下异常:

2012-09-18 10:45:33.202 testStoryboard[5792:14203] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Requesting the window of a view (<UITableViewCell: 0x6b80190; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; autoresize = W; layer = (null)>) with a nil layer. This view probably hasn't received initWithFrame: or initWithCoder:.' 
*** First throw call stack:
(0x14b2022 0xeb2cd6 0x145aa48 0x145a9b9 0x4b7c4 0x4d612 0x5246f 0x4c01b 0x6c494 0x9c838 0xb00e6 0xb03ce 0x9bcbd 0xaa6f1 0x53d21 0x14b3e42 0x1d83679 0x1d8d579 0x1d124f7 0x1d143f6 0x1da1160 0x25f30 0x148699e 0x141d640 0x13e94c6 0x13e8d84 0x13e8c9b 0x139b7d8 0x139b88a 0x15626 0x20fd 0x2065)
terminate called throwing an exception(lldb)

编辑:这是我的代码:

//  SecondTableViewController.m
#import "SecondTableViewController.h"
@interface SecondTableViewController ()
@end
@implementation SecondTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
     self = [super initWithStyle:style];
     if (self) {
        // Custom initialization
     }
     return self;
}
- (void)viewDidLoad
{
   [super viewDidLoad];
   //Initialize the array.
   myArray2 = [[NSMutableArray alloc] init];
   //Add items
   [myArray2 addObject:@"Iceland"];
   [myArray2 addObject:@"Greenland"];
   [myArray2 addObject:@"Switzerland"];
   [myArray2 addObject:@"Norway"];
   [myArray2 addObject:@"New Zealand"];
   [myArray2 addObject:@"Greece"];
   [myArray2 addObject:@"Rome"];
   [myArray2 addObject:@"Ireland"];
   //Set the title
   self.navigationItem.title = @"Countries";
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:   (UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [myArray2 count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [UITableViewCell alloc];
    }
    cell.textLabel.text = [myArray2 objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}
@end

我是IOS的新手,有人可以让我朝着正确的方向前进吗?

你对

单元的启动还没有完成,你必须初始化单元,而不仅仅是分配它。

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

最新更新