【问题标题】:Run-Time Error for TableViewDataSource file is separatedTableViewDataSource 文件的运行时错误已分离
【发布时间】:2012-03-23 07:44:37
【问题描述】:

当我分离 TableView 和 TableViewDataSource 文件中包含的 ViewController 时, 我收到一个运行时错误:“..EXC_BAD_ACCESS ..”。

下面有完整的来源。

// ViewController file
<ViewController.h>

@interface ViewController : UIViewController <UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
@end

<ViewController.m>

- (void)viewDidLoad
{
    **DS1 *ds = [[DS1 alloc] init];**        
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 200) style:UITableViewStylePlain]; 
    _tableView.delegate = self;
    **_tableView.dataSource = ds;**
    [self.view addSubview:_tableView];
}



// TableViewDataSource file 

<DS1.h>
@interface DS1 : NSObject <UITableViewDataSource>
@property (strong, nonatomic) NSArray *dataList;
@end


<DS1.m>
#import "DS1.h"

@implementation DS1
@synthesize dataList = _dataList;

- (id)init
{
    self = [super init];    
    if (self) {        
        _dataList = [NSArray arrayWithObjects:@"apple",@"banana", @"orange", nil]; 
    }
    return self;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }

    cell.textLabel.text = [_dataList objectAtIndex:indexPath.row];

    return cell;
}

@end

如果我从

更改 ViewController.m 的代码
     _tableView.dataSource = ds; 

     _tableView.dataSource = self;

,那就没问题了。 (当然,在 DataSource 方法被附加到 ViewController.m 之后)

我找不到任何问题,请帮助我并提前感谢。

【问题讨论】:

    标签: iphone ios xcode datasource tableview


    【解决方案1】:

    如果这是 ARC,您必须为您的数据源创建一个实例变量或 @property
    您将 dataSource ds 分配为局部变量。但是tableView的dataSource属性没有保留ds。所以在 viewDidLoad 结束时,ARC 将释放 ds 并被释放。

    将 ds 保存为 viewController 的属性。像这样:

    @interface ViewController : UIViewController <UITableViewDelegate>
    @property (strong, nonatomic) UITableView *tableView;
    @property (strong, nonatomic) DS1 *dataSource;
    @end
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];  // <-- !!!
        self.dataSource = [[DS1 alloc] init];
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 200) style:UITableViewStylePlain]; 
        _tableView.delegate = self;
        _tableView.dataSource = self.dataSource;
        [self.view addSubview:_tableView];
    }
    

    【讨论】:

    • 非常感谢。没关系,现在
    • 这快把我逼疯了...有没有办法调试这些崩溃?
    猜你喜欢
    • 1970-01-01
    • 2018-10-16
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多