Я пытаюсь создать приложение для 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