我正在尝试启动一个新的 viewController,里面有一个 tableView,但我一直收到 EXC_BAD_ACCESS 错误。
.h
#import <UIKit/UIKit.h>
@interface GeneralInfoViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
@end
.m
@interface GeneralInfoViewController ()
@end
@implementation GeneralInfoViewController
{
NSArray *tableData;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.title = @"Allmänna uppgifter";
[super viewDidLoad];
// Initialize table data
tableData = [NSArray arrayWithObjects:@"Egg Benedict", @"Mushroom Risotto", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)dealloc {
[super dealloc];
}
- (void)viewDidUnload {
[super viewDidUnload];
}
@end
我在 numberOfRowsInSection
方法的 return [tableData count];
行中遇到异常,但我认为问题与我如何启动viewDidLoad
和 initWithNibName
中的 viewController 但我不知道它是什么。
我试图将 viewDidLoad
与 loadView
交换,但这只是产生了一个不同的错误。
提前致谢!
最佳答案
您已经使用便捷方法(即没有 allo/init/retain)初始化了 tableData。所以数组会自动释放。
尝试使用
tableData=[[NSArray alloc] initWithObjects:...];
关于iphone - 使用 tableView 启动新 View 时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017816/