我正在制作一个应用程序,它试图从.plist文件(放在那里解析JSON)中读取信息。应用程序(tableview)在滚动时崩溃
从文件中读取流动性很好:得到了字典数组,但试图在tableview上显示它时,问题就开始了。初始视图加载正常,但是当我开始滚动时,应用程序崩溃。
#define DOCUMENTS [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *filePathDocArray = [DOCUMENTS stringByAppendingPathComponent:@"filters.plist"];
NSString *filePathBundleArray = [[NSBundle mainBundle] pathForResource:@"filters" ofType:@"plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:filePathDocArray]) {
[[NSFileManager defaultManager] copyItemAtPath:filePathBundleArray toPath:filePathDocArray error:nil];
NSLog(@"File saved");
} else {
NSLog(@"File already exists");
filters = [NSArray arrayWithContentsOfFile:filePathDocArray];
}
}
这里我得到我需要进入过滤器阵列中的所有信息(通过循环检查)。然后:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [filters count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *myIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:myIdentifier];
}
NSInteger crow = indexPath.row;
NSDictionary *story = [filters objectAtIndex: crow];
cell.textLabel.text = [story objectForKey:@"Name"];
cell.detailTextLabel.text = [story objectForKey:@"Description"];
return cell;
}
@end
当应用程序启动everething是OK:我看到normel表视图,但是当我开始滚动它崩溃
系列断点后调试我评估,该应用程序后,启动模拟器上的链接过滤器螺丝,所以当我尝试填充下一个单元格时,故事字典无法正确创建。
它可能是一个什么样的问题?
这里控制台报告:
2012-09-22 13:37:43.545 JSONExample[4559:207] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0
2012-09-22 13:37:43.547 JSONExample[4559:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x6a083c0'
谢谢。不,我不使用ARC。我应该怎么做才能使过滤器保持正确?我有@property(非原子,保留)NSArray *过滤器;在头文件中。应该在哪里添加autorelease?再次感谢您 –
是的,这是正确的保留和非原子的头..然后在实现文件中合成它,你的单元格代码应该看起来像这样。 –
保留天气... thx –