iphone - 使用 tableView 启动新 View 时出现 EXC_BAD_ACCESS

标签 iphone ios objective-c uiviewcontroller tableview

我正在尝试启动一个新的 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]; 行中遇到异常,但我认为问题与我如何启动viewDidLoadinitWithNibName 中的 viewController 但我不知道它是什么。

我试图将 viewDidLoadloadView 交换,但这只是产生了一个不同的错误。

提前致谢!

最佳答案

您已经使用便捷方法(即没有 allo/init/retain)初始化了 tableData。所以数组会自动释放。

尝试使用

tableData=[[NSArray alloc] initWithObjects:...];

关于iphone - 使用 tableView 启动新 View 时出现 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017816/

相关文章:

c++ - 像 C++ 一样支持 NSArray(奇怪的代码示例)

iphone - 如何知道应用程序是否被用户或 iOS 终止(后台 10 分钟后)

iphone - willTransitionToState 和 didTransitionToState 没有被调用

ios - 使用 Project_Prefix.pch 导入我的单例类 - 不好的做法?

ios - 使用参数发出 nsurlsession 请求

IOS 7 UITextField resignFirstResponder 坏

iphone - 使用 iCarousel 缩放项目

iphone - 使用 NSXMLParser 解析 XML 时出现问题

ios - 以编程方式滑动 UIScrollView

iOS tableview customcell - 屏幕外单元格上的控件有时不可见