为什么 NSUserDefault 中存储为对象的 Double 值不能反映在 UITableViewCell 中?
我正在尝试将 Double
值存储在 NSUserDefault
中,但是尽管我能够存储它(因为我的 NSLog 值显示真实值),但当我尝试重新加载 UITableView 时,它的 Cell值不会更新为 userdefault 中的当前值。
仅当我从 UIAlertView
的委托方法调用我的 setUserDefaults
方法时,才会发生这种奇怪的行为
。 为什么会发生这种奇怪的行为?
这是我的代码:
- (void)setUserDefaults
{
NSLog(@"setUserDefault : empArray: %d, empCount: %d",empArray.count, empCount);
NSMutableDictionary *empData = [[NSMutableDictionary alloc] init];
if (empArray.count>1)
empData = [empArray objectAtIndex:(empCount-1)]; // because empCount starts from 1. and empArray[0] = empCount 1
else
empData = [empArray objectAtIndex:0];
[userDefault setObject:[NSString stringWithFormat:@"%.2f",[empData objectForKey:@"salary"]] forKey:@"salary"];
NSLog(@"setUserDefaults: salary=%.2f",[[empData objectForKey:@"salary"] doubleValue]);
[empData release];
[self.tblView reloadData];
}
UIAlertView 的委托方法如下:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex
{
if (alertView.tag == 1) // btnRemoveEmpPressed.
{
if(buttonIndex==0)
{
NSLog(@"buttonIndex 0");
}
else
{
NSLog(@"empRemovedPressed OK, buttonIndex 1, empArray Count %d, empCount %d",empArray.count, empCount);
// [empArray removeObjectAtIndex:(empCount)];
empCount -= 1;
lblTitle.text = [NSString stringWithFormat:@"Employee %d",empCount];
[self setUserDefaults];
}
// [tblView reloadData];
}
else if (alertView.tag == 2)
{
if (buttonIndex==1)
{
[self resetUserDefaults];
empCount = 1;
[empArray removeAllObjects];
lblTitle.text = [NSString stringWithFormat:@"Employee %d",empCount];
}
}
}
I am trying to store Double
Value in NSUserDefault
But though i am able to store it (as my NSLog value shows true value), when I tried to reload UITableView, its Cell value is not updated with current value in userdefault.
This weird behavior happens only when i call my setUserDefaults
method from delegate method of UIAlertView
Why such weird behavior happens??
Here is my code:
- (void)setUserDefaults
{
NSLog(@"setUserDefault : empArray: %d, empCount: %d",empArray.count, empCount);
NSMutableDictionary *empData = [[NSMutableDictionary alloc] init];
if (empArray.count>1)
empData = [empArray objectAtIndex:(empCount-1)]; // because empCount starts from 1. and empArray[0] = empCount 1
else
empData = [empArray objectAtIndex:0];
[userDefault setObject:[NSString stringWithFormat:@"%.2f",[empData objectForKey:@"salary"]] forKey:@"salary"];
NSLog(@"setUserDefaults: salary=%.2f",[[empData objectForKey:@"salary"] doubleValue]);
[empData release];
[self.tblView reloadData];
}
Delegate method of UIAlertView goes as below:
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex
{
if (alertView.tag == 1) // btnRemoveEmpPressed.
{
if(buttonIndex==0)
{
NSLog(@"buttonIndex 0");
}
else
{
NSLog(@"empRemovedPressed OK, buttonIndex 1, empArray Count %d, empCount %d",empArray.count, empCount);
// [empArray removeObjectAtIndex:(empCount)];
empCount -= 1;
lblTitle.text = [NSString stringWithFormat:@"Employee %d",empCount];
[self setUserDefaults];
}
// [tblView reloadData];
}
else if (alertView.tag == 2)
{
if (buttonIndex==1)
{
[self resetUserDefaults];
empCount = 1;
[empArray removeAllObjects];
lblTitle.text = [NSString stringWithFormat:@"Employee %d",empCount];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 NSUserDefaults 的 setDouble:forKey: 方法并让它为您管理该值。另外,还要
同步
NSUserDefaults
以保存该值以供以后使用。You should use the
setDouble:forKey:
method ofNSUserDefaults
and let it manage the value for you. Alsosynchronize
theNSUserDefaults
in order to save the value for later usage.doubleValue
。要么这样做,要么如 Herz 所说,使用 setDouble 方法。synchronize
userDefault
是在其他地方设置的,并且它你使用的时候不是nil
吗?doubleValue
when setting user defaults. Either do that or, as herz says, use thesetDouble
method.synchronize
on your defaults object after updating ituserDefault
is set up elsewhere and it is notnil
when you are using it?