简体   繁体   English

为什么UITableViewCell中不能反映NSUserDefault中存储为Object的Double值?

[英]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. 您应该使用NSUserDefaultssetDouble:forKey:方法,并让它为您管理值。 Also synchronize the NSUserDefaults in order to save the value for later usage. synchronize NSUserDefaults以便保存该值以备后用。

  • You aren't using doubleValue when setting user defaults. 设置用户默认值时,您没有使用doubleValue Either do that or, as herz says, use the setDouble method. 要么执行此操作,要么如herz所说,使用setDouble方法。
  • You aren't calling synchronize on your defaults object after updating it 更新对象后,您不会在其上调用“ synchronize
  • Don't release empData, you didn't retain it 不要发布empData,您没有保留它
  • I'm assuming 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM