[英]Why can't a Double value stored as Object in NSUserDefault is not reflected in UITableViewCell?
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. 我正在尝试将
Double
Value存储在NSUserDefault
但是尽管我能够存储它(因为我的NSLog值显示真实值),但是当我尝试重新加载UITableView时,其Cell值未使用userdefault中的当前值进行更新。
This weird behavior happens only when i call my setUserDefaults
method from delegate method of UIAlertView
仅当我从
UIAlertView
委托方法调用setUserDefaults
方法时,才会发生这种奇怪的行为
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: 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];
}
}
}
You should use the setDouble:forKey:
method of NSUserDefaults
and let it manage the value for you. 您应该使用
NSUserDefaults
的setDouble:forKey:
方法,并让它为您管理值。 Also synchronize
the NSUserDefaults
in order to save the value for later usage. 还
synchronize
NSUserDefaults
以便保存该值以备后用。
doubleValue
when setting user defaults. doubleValue
。 Either do that or, as herz says, use the setDouble
method. setDouble
方法。 synchronize
on your defaults object after updating it synchronize
” userDefault
is set up elsewhere and it is not nil
when you are using it? userDefault
在其他地方设置,并且在使用时不是nil
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.