1

我正在制作一个尝试从.plist文件中读取信息的应用程序(放在那里解析的JSON)。

从文件中读取的流程很好:得到了字典数组,但是在尝试在 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

当应用程序启动时一切正常:我看到了 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'

4

4 回答 4

0

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

在您调用时,您的“过滤器”变量是一个 NSString/CFString objectAtIndex:- 而不是您假设的数组。链接到的问题中给出的解决方案是设置retain您的过滤器数组。

于 2012-09-22T10:06:56.800 回答
0

你在用ARC吗??如果没有,那么首先自动释放单元格!并且如果过滤器不是保留属性..请使其成为一个并合成它,如果没有使用ARC,则将其释放到dealloc块中..我想应该这样做..

于 2012-09-22T10:10:39.823 回答
0

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

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

于 2012-09-22T10:38:15.233 回答
0

您需要记住另一件事..如果您执行此步骤

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

每次调用 cellForRowAtIndexPath 时,保留计数都会增加 1。但您只会对它进行一次 delloc。因此,您的应用程序中会出现内存泄漏。我建议您阅读一次内存管理指南。它是一个小文件。最多需要你一天的时间。但是会更加自信地知道什么该做,什么不该做。

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

干杯!!快乐编码!

于 2012-09-22T11:52:07.667 回答