2012-09-22 59 views
1

我正在制作一个应用程序,它试图从.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'

回答

0

你使用ARC吗? 如果不是那么先自动释放细胞! 如果过滤器不是一个保留属性..请让它合并一次,如果ARC没有使用然后释放它在dealloc块..应该这样做,我想..

+0

谢谢。不,我不使用ARC。我应该怎么做才能使过滤器保持正确?我有@property(非原子,保留)NSArray *过滤器;在头文件中。应该在哪里添加autorelease?再次感谢您 –

+0

是的,这是正确的保留和非原子的头..然后在实现文件中合成它,你的单元格代码应该看起来像这样。 –

+0

保留天气... thx –

0

-[NSCFString objectAtIndex:]: unrecognized selector似乎是同样的问题。

当您拨打objectAtIndex:时,您的'filters'变量是NSString/CFString - 而不是数组,就像您所假设的那样。问题中给出的解决方案是retain您的过滤器数组,只要它被设置。

+0

我把它设为@property(nonatomic,retain)NSArray * filters;在头文件中...我应该改变什么?谢谢 –

+0

正如我所链接到的问题所述,在分配它时必须“保留”你的过滤器变量。在这种情况下,将'-viewDidLoad'中的行替换为'filters = [[NSArray arrayWithContentsOfFile:filePathDocArray] retain];' – ttarik

+0

谢谢!刚添加[[...] retain]到'filters = [NSArray arrayWithContentsOfFile:filePathDocArray];'所以它变成'filters = [[NSArray arrayWithContentsOfFile:filePathDocArray] retain];'一切正常! –

0

如果你wote属性为在filters@property (nonatomic, retain) NSArray *filters;你需要写像self.filters = [NSArray arrayWithContentsOfFile:filePathDocArray];

否则你需要写像filters = [[NSArray arrayWithContentsOfFile:filePathDocArray] retain];

+0

是否这样做 –

0

你需要记住的另一件事.. ,如果你做到这一步

NSDictionary *story = [[filters objectAtIndex: crow]retain]; 

每次调用cellForRowAtIndexPath时,保留计数都会增加1.但是您只会删除一次。 所以你的应用程序会有内存泄漏。 我建议你一次通过内存管理指南。它的一个小文件。在最大限度内它将需要一天的时间。但是,对于该做什么和不该做什么,会感到更加自信。

http://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/CFMemoryMgmt.pdf

干杯!和快乐的编码!

+0

Thx!但我保留不是字典,但阵列(过滤器)只有一次 –

+0

好吧..它不能是一个gr8担心,然后......最好的运气和快乐的编码! –