滚动 TableView 时应用程序崩溃

发布于 2024-12-07 22:50:39 字数 2152 浏览 0 评论 0原文

当我滚动 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;    
}

My app is crashing when i scroll my TableView. First in my viewDidLoad method a load a dictionary from a file and for this dictionary i enumerate all keys.

 - (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"];
}

In my cellForRowAtIndexPath i populate cells with data from that dictionary. Anyway when my app starts everything looks fine, first five rows are drawn correctly, but when i start to scroll my app crash. My idea is that the problem is with autoreleased object here, i tried to retain them and after using them to release ,but unsuccessful. DEBUGGER SHOWS THAT MY PROBLEM IS AT LINE WITH BOLD

// 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;    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北凤男飞 2024-12-14 22:50:39

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

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

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

As a result from the discussion, [banks objectForKey:@"Last Updated"] gives you a NSString, not a NSDictionary!

You could get around this error by doing

if ([[banks objectForKey:currentCurrency] class] == [NSDictionary class]) { 
    ... rest of the code here .. 
}
半城柳色半声笛 2024-12-14 22:50:39

使用下面的代码更改您的 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"];
 }

Change your viewDidLoad with below code it will work

- (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"];
 }
唠甜嗑 2024-12-14 22:50:39

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

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

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

-[NSCFString objectForKey:]: unrecognized selector sent to instance
0x4bab9c0

Your banks and keys variables aren't retained, as mentioned in another answer, but this isn't the error.

As per this error, your fetchedCurrency object is an NSString, not an NSDictionary. Check the format of your currency.archive file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文