簡體   English   中英

NSMutableArray加載到UITableView崩潰滾動

[英]Loading NSMutableArray Into UITableView Crashes On Scroll

我正在嘗試將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];

問題是這樣的:

_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"]];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM