滚动TableView时应用程序崩溃

时间:2011-10-05 14:04:08

标签: iphone ios uitableview

当我滚动我的TableView时,我的应用程序崩溃了。首先在我的viewDidLoad方法中,从文件中加载字典,对于这个字典,我枚举所有键。

 - (void)viewDidLoad {

     [super viewDidLoad];

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];      

     path = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"currency.archive"]]; 

     banks = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

     keys = [banks allKeys];

     // set date for last update 
     dayMonthYear.text = [banks objectForKey:@"Last Updated"];
}

在我的cellForRowAtIndexPath中,我使用该字典中的数据填充单元格。无论如何,当我的应用程序启动一切看起来很好,前五行正确绘制,但当我开始滚动我的应用程序崩溃。我的想法是问题是在这里使用自动释放的对象,我试图保留它们并在使用它们之后释放,但是不成功。调试人员表示我的问题与大胆相符

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell %d_%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {      

        [[NSBundle mainBundle] loadNibNamed:@"CurrencyTableCell" owner:self options:nil];

        cell = currencyTableCell;

        //don't show selected cell
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //set height
        self.cellHeight = cell.frame.size.height;
    }    

    // Fetch currency 
    NSString *currentCurrency = [keys objectAtIndex:indexPath.row];

    NSDictionary *fetchedCurrency = [banks objectForKey:currentCurrency];

    **NSString *name = [fetchedCurrency objectForKey:@"Currency Name"];**

    currencyTitle.text = name;

    NSString *charCode = [fetchedCurrency objectForKey:@"Code"];

    currencyCode.text = charCode;

    NSString* formattedNumber = [NSString stringWithFormat:@"%.02f",[[fetchedCurrency  objectForKey:@"Value"] floatValue]];

    if ([formattedNumber length] == 4) {
        formattedNumber = [NSString stringWithFormat:@"%@%@",@"0",formattedNumber];
    }

    buyPrice.text = formattedNumber;

    return cell;    
}

3 个答案:

答案 0 :(得分:0)

使用以下代码更改viewDidLoad

- (void)viewDidLoad {
     [super viewDidLoad];

     NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];      
     path = [rootPath stringByAppendingPathComponent:[NSString stringWithFormat:@"currency.archive"]]; 
     banks = [[NSDictionary alloc] initWithDictionary:[NSKeyedUnarchiver unarchiveObjectWithFile:path]];
     keys = [[NSArray alloc] initWithArray:[banks allKeys]];
     // set date for last update 
     dayMonthYear.text = [banks objectForKey:@"Last Updated"];
 }

答案 1 :(得分:0)

作为讨论的结果,[banks objectForKey:@"Last Updated"]为您提供了NSString,而不是NSDictionary!

你可以通过

解决这个错误
if ([[banks objectForKey:currentCurrency] class] == [NSDictionary class]) { 
    ... rest of the code here .. 
}

答案 2 :(得分:0)

  

- [NSCFString objectForKey:]:发送到实例的无法识别的选择器   0x4bab9c0

您的银行和密钥变量不会保留,如另一个答案所述,但这不是错误。

根据此错误,您的fetchedCurrency对象为NSString,而非NSDictionary。检查currency.archive文件的格式。

相关问题