iphone - 滚动 TableView 时应用程序崩溃

标签 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;    
}

最佳答案

作为讨论的结果,[banks objectForKey:@"Last Updated"] 给你一个 NSString,而不是 NSDictionary!

您可以通过执行以下操作来解决此错误

if ([[banks objectForKey:currentCurrency] class] == [NSDictionary class]) { 
    ... rest of the code here .. 
}

关于iphone - 滚动 TableView 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7662551/

相关文章:

iphone - NSMutableArray 没有将值传递给 CellForRowAtIndexPath?

iphone - 应用程序在dealloc中崩溃

iPhone:拖动以创建新对象

ios - TableView 中的可扩展 View

ios - 为什么我们在 fetchRequest() 声明之前放置 @nonobjc 属性?

ios - 如何让后退按钮返回到 UITableView 中的相同位置

ios - 选择单元格后 View 在不同单元格上重复

iphone - 像表格 View 单元格一样绘制游戏中心

ios - UITableView:单击单元格并使用 UIWebView 打开单元格中的链接

iphone - 滚动时 UITableView 崩溃