简体   繁体   English

NSMutableArray加载到UITableView崩溃滚动

[英]Loading NSMutableArray Into UITableView Crashes On Scroll

I'm trying to load my NSMutableArray into a UITableView but it crashes as soon as you scroll. 我正在尝试将NSMutableArray加载到UITableView中,但是一旦滚动它就会崩溃。 The data loads into the UITableView but like I said I can't scroll. 数据加载到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 is retained and it is non-atomic, so I should be good to go. MyArray被保留,并且它不是原子的,所以我应该很好。 Maybe something is dying though before the UITableView can use it? 也许在UITableView可以使用它之前,某些东西已经死了吗?

The error that I am getting is as follows: 我收到的错误如下:

EXC_BAD_ACCESS @ this line - NSString *cellValue = [_myArray objectAtIndex:indexPath.row]; EXC_BAD_ACCESS @此行NSString *cellValue = [_myArray objectAtIndex:indexPath.row];

The problem is this: 问题是这样的:

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

You are not accessing your setter so the retain isn't happening. 您没有访问您的二传手,因此保留没有发生。 You want: 你要:

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

or 要么

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

or 要么

[_myArray release];
_myArray = [[NSMutableArray alloc] initWithArray:[myString componentsSeparatedByString:@"\n"]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM