2012-09-18 23 views
1

創建第二tableviewcontroller後,我在已連接TOT此的tableView的tabbarItem當點擊了以下異常:如何解決創建tableview後的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,有人能讓我朝着正確的方向發展嗎?

+0

向我們顯示您的代碼。 –

+0

並向我們展示異常回溯。 –

+0

@JonathanGrynspan我剛剛編輯我的問題,提前致謝。 – RTB

回答

5

您的單元格啓動不完整,您必須初始化單元格,而不僅僅是分配它。

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

這似乎是正確的。不要忘記autorelease,如果你不使用ARC – Mike

+0

@Scar我的上帝,那是weard。你的anwser修復了它,但是在我的第一個tableViewController中,我沒有這個代碼,但是它和上面的代碼一起工作很好。 – RTB