2012-03-23 17 views
1

Когда я выделил ViewController, включенный в файлы TableView и TableViewDataSource, У меня возникла ошибка во время выполнения: «..EXC_BAD_ACCESS ..».Ошибка времени выполнения для файла TableViewDataSource разделяется

Существует целый источник ниже.

// 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)

Я не могу найти никаких проблем, помогите мне и спасибо заранее.

ответ

1

Если это ARC, вам нужно создать переменную экземпляра или @property для вашего источника данных.
В качестве локальной переменной вы выделяете свой источник данных ds. Но свойство tableView не сохраняет ds. Поэтому в конце viewDidLoad ARC выпустит ds, и он будет освобожден.

save ds как собственность вашего видаController. например:

@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]; 
} 
+0

Большое вам спасибо. Это нормально, теперь – user1287710

+0

Это сводило меня с ума ... Есть ли способ отладить эти аварии? – NSExplorer

Смежные вопросы