滚动 TableView 时应用程序崩溃
当我滚动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
讨论的结果是,
[banks objectForKey:@"Last Updated"]
为您提供了一个 NSString,而不是 NSDictionary!您可以通过执行以下操作来解决此错误
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
使用下面的代码更改您的 viewDidLoad 它将起作用
Change your viewDidLoad with below code it will work
正如另一个答案中提到的,您的银行和密钥变量不会保留,但这不是错误。
根据此错误,您的
fetchedCurrency
对象是NSString
,而不是NSDictionary
。检查您的currency.archive 文件的格式。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 anNSString
, not anNSDictionary
. Check the format of your currency.archive file.