我正在尝试创建一个使用标签栏控制器的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 :(得分:1)
如果您使用Markhe评论时使用降价格式发布代码,将会更容易提供帮助。我通过快速扫描确实注意到了一些事情。我也无法从中了解您在界面构建器中创建的内容以及是否正确配置了UITabBarController和所有插座。除了以下内容之外,您可能还有其他事情,但这是一个开始。
确保释放您保留的任何内容。例如,在viewDidLoad中,您分配array
并永远不会释放它。
同样,不要发布未保留的内容。在initPersons
中,您使用返回自动释放对象的数组构造函数创建可变数组aPersons
。然后你有[aPersons release]
。这将导致崩溃b / c您正在释放未保留的对象。
在viewDidUnload
和dealloc
中正确清理。在这两个版本中,您需要发布tableView
。在dealloc
,您有[tableView dealloc]
。那应该是[tableView release]
您的数据源创建过于复杂,但我确实看到了明显的问题。您在self.persons
中设置了initPersons
,这很好。然后,您对数组进行排序并将其存储在sortedArray
中。假设您的意图是保持数组排序,您当前正在丢弃已排序的数组。 self.persons
仍然是未分类的版本。虽然这不是崩溃,但我怀疑这是你的意图。
您需要实施tableView:cellForRowAtIndexPath:
如果遗漏,这绝对会导致崩溃。假设您正确配置了tableView委托和dataSource。您应该将协议UITableViewDataSource
和UITableViewDelegate
添加到SecondViewController
的接口定义中,这样您就可以获得有关实现所需协议方法的正确编译器警告。
答案 1 :(得分:0)
- tableView:cellForRowAtIndexPath:是UITableviewDatasource protocol的必需方法。所以启用it.be happy
如果你不想在其中实现任何实现,那么只需留下空白定义。
{ 返回细胞; }