2011-04-05 47 views
0

我正嘗試創建一個使用標籤欄控制器的iPhone應用程序。第一個標籤工作正常。爲什麼我的標籤欄控制器崩潰?

但是,當我點擊標籤欄中的第二個選項卡時,整個應用程序崩潰。我正試圖在第二個選項卡中實現表格視圖。可能導致崩潰的原因是什麼?

這裏是我的代碼:

SecondViewController.h

#import <UIKit/UIKit.h> 

@class Person; 

@interface SecondViewController : UIViewController<UITableViewDelegate, UITableViewDataSource >{ 
    UITableView *tableView; 
    NSArray *persons; 

} 
@property (nonatomic, retain) IBOutlet UITableView *tableView; 
@property (nonatomic,retain) NSArray *persons; 
-(void)initPersons:(NSArray *) array; 

@end 

SecondViewController.m

#import "SecondViewController.h" 
#import "Person.h" 


@implementation SecondViewController 

@synthesize tableView; 
@synthesize persons; 

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
/* 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization. 
    } 
    return self; 
} 
*/ 

- (id)init { 

    if (self = [super initWithNibName:@"SecondViewController" bundle:nil]) { 

     //self.title = @"Slot"; 



     UIImage* anImage = [UIImage imageNamed:@"cherry.png"]; 

     UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"table" image:anImage tag:0]; 

     self.tabBarItem = theItem; 

     [theItem release]; 

    } 

    return self; 

} 

-(void)initPersons:(NSArray *) array{ 
    int size = [array count]; 
    int i = 0; 
    NSMutableArray *aPersons = [NSMutableArray array]; 
    while (i < size) { 
     Person *person = [[Person alloc]init]; 
     NSString * name =[array objectAtIndex:i]; 
     NSArray *chunks =[name componentsSeparatedByString: @" "]; 
     person.firstName = [chunks objectAtIndex:0]; 
     person.lastName = [chunks objectAtIndex:[chunks count]-1]; 
     [aPersons addObject:person]; 

     [person release]; 
     i++; 
    } 
    self.persons=aPersons; 
    [aPersons release]; 

} 

-(NSArray *)sortArray { 
    NSSortDescriptor *lastNameDescriptor = [[[NSSortDescriptor alloc] 

          initWithKey:@"lastName" 

          ascending:YES 

          selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 

    NSSortDescriptor *firstNameDescriptor = [[[NSSortDescriptor alloc] 

          initWithKey:@"firstName" 

          ascending:YES 

          selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]; 

    NSArray *sortDescriptors = [NSArray arrayWithObjects:lastNameDescriptor, 

         firstNameDescriptor, nil]; 

    return [persons sortedArrayUsingDescriptors:sortDescriptors]; 
} 


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    NSArray *array = [[NSArray alloc] initWithObjects: 
         @"Amin Alrusayni", @"Berksan Ates", 
         @"Becca Bedell", @"Joseph Carioti", 
         @"Christopher Conry", @"Jeremy Dobbins", @"Timothy Fox", 
         @"Eric Green", @"Timothy Gruscinski", @"Daniel Gur", 
         @"Yousef Guzaiz", @"Tyler Herzog", @"Alicia Johnson", @"Scott Kazakis", 
         @"Nathan Kidwell", @"Dakota Kincer", @"Scott Moore", 
         @"Sean Reber", @"Michael Romeo", @"Alexander Shibble", 
         @"Joshua Shipley", @"Mitchell Slemc", @"Thomas Smith", 
         @"Christopher Wagner", @"Corey Zachrich", @"Ahmed Alalawi", 
         @"Abdullah Alqahtani", @"Daniel Angelis", @"Brian Bartman", 
         @"William Haverstock", @"Hui Hong", @"Rong Li", 
         @"Amitkumar Mali", @"Christian Newman", nil]; 

    [self initPersons:array]; 
    NSArray *sortedArray = [self sortArray]; 
    for (Person *person in sortedArray) 
    { 
     NSString *fullName = [[person.firstName stringByAppendingString:@" "] stringByAppendingString:person.lastName]; 
     NSLog(@"%@",fullName); 
     NSLog(@" "); 
    } 
    [super viewDidLoad]; 
} 


/* 
// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations. 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 
*/ 
//commented out this function 
/* 
- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc. that aren't in use. 
}*/ 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [tableView dealloc]; 
    [super dealloc]; 
} 

#pragma mark - 
#pragma mark TableView DataSource 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [self.persons count]; 
    } 

/*- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 






    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:SimpleTableIdentifier] autorelease]; 
    } 

    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [persons objectAtIndex:row]; 
    return cell; 

}*/ 
@end 
+0

控制檯或崩潰日誌在崩潰時會說什麼? – mipadi 2011-04-05 17:55:27

+0

歡迎來到StackOverflow!請注意格式和拼寫。如果你這樣做,人們會很樂意幫助你。另外,我建議閱讀[FAQ](http://stackoverflow.com/faq)來開始。那就是說,你有什麼錯誤? – Moshe 2011-04-05 17:55:30

回答

0

- 的tableView:的cellForRowAtIndexPath:UITableviewDatasource protocol.so是必需的方法讓它開心

如果你不想在它裏面有任何的實現,那就留下空白的定義。

return cell; }

+0

這是不正確的。 tableView:cellForRowAtIndexPath:**必須**返回一個有效的UITableViewCell實例。 – XJones 2011-04-05 18:42:04

1

如果您使用降價格式將您的代碼發佈爲Moshe評論,那將會更容易。我通過快速掃描發現了一些事情。我也無法從此告訴你在界面構建器中創建了什麼,以及UITabBarController和所有插座是否已正確配置。除了以下內容,您可能還有其他事情正在進行,但這是一個開始。

  1. 確保您釋放任何您保留的內容。例如,在viewDidLoad中,您分配了array並且從不釋放它。

  2. 同樣,不要發佈你沒有保留的東西。在initPersons中,使用返回自動釋放對象的數組構造函數創建可變數組aPersons。您然後有[aPersons release]。這將導致崩潰b/c你正在釋放一個你沒有保留的對象。

  3. viewDidUnloaddealloc中正確清理。在這兩個你需要發佈tableView。在dealloc你有[tableView dealloc]。這應該是[tableView release]

  4. 您的數據源創建過於複雜,但我確實看到一個明確的問題。你正在設置self.personsinitPersons這很好。您然後排列數組並將其存儲在sortedArray中。假設你的意圖是保持數組排序,你目前正在丟棄排序後的數組。 self.persons仍然是未排序的版本。雖然這不是崩潰,但我懷疑這是你的意圖。

  5. 您需要執行tableView:cellForRowAtIndexPath:這絕對會導致崩潰,如果丟失。這是假設你有正確配置的tableView委託和數據源。您應該將協議UITableViewDataSourceUITableViewDelegate添加到SecondViewController的接口定義中,以便您可以獲得有關實現所需協議方法的正確編譯器警告。