0

NSMutableArray を UITableView に読み込もうとしていますが、スクロールするとすぐにクラッシュします。データは 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];

4

1 に答える 1

5

問題はこれです:

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

セッターにアクセスしていないため、保持は行われません。あなたがしたい:

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"]];
于 2013-10-15T17:04:10.150 に答える