2013-10-15 11 views
0

Я пытаюсь загрузить мой NSMutableArray в UITableView, но он вылетает, как только вы прокручиваете. Данные загружаются в UITableView, но, как я уже сказал, я не могу прокручивать.Загрузка NSMutableArray в UITableView сбой при прокрутке

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize myArray = _myArray; 

#pragma mark TableViewStuff 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return _myArray.count; 
} 


//—-insert individual row into the table view—- 
- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    //—-try to get a reusable cell—- 
    UITableViewCell *cell = 
    [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    //—-create new cell if no reusable cell is available—- 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier] 
       autorelease]; 
    } 

    //—-set the text to display for the cell—- 
    NSString *cellValue = [_myArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 

    return cell; 
} 
#pragma mark LifeCycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    // Load the file into a string 
    NSString* filePath = [[NSBundle mainBundle] pathForResource:@"listOfColleges" ofType:@"txt"]; 
    NSString* myString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
    //Fill the array with subsets of the string 
    _myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)dealloc 
{ 
    [_myArray release]; 
    [super dealloc]; 
} 

@end 

MyArray сохранен и не является атомарным, поэтому я должен быть хорошим. Может быть, что-то умирает, хотя UITableView может его использовать?

ошибка, что я получаю выглядит следующим образом:

EXC_BAD_ACCESS @ этой линии - NSString *cellValue = [_myArray objectAtIndex:indexPath.row];

+1

Вы должны установить массив 'self.myArray =' поэтому ваш определенный метод свойство аксессор получает шанс бежать. – Wain

ответ

5

Проблема заключается в следующем:

_myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]]; 

Вы не доступа к вашей сеттер поэтому сохранить ISN Не происходит. Вы хотите:

self._myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]]; 

или

[_myArray release]; 
_myArray = [[NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]] retain]; 

или

[_myArray release]; 
_myArray = [[NSMutableArray alloc] initWithArray:[myString componentsSeparatedByString:@"\n"]]; 
Смежные вопросы