Mik*_* D. 0 iphone objective-c uitableview ios
我正在尝试将我的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"]];
您没有访问您的setter,因此保留不会发生.你要:
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"]];
| 归档时间: | 
 | 
| 查看次数: | 220 次 | 
| 最近记录: |