0
[array addObject:textdata.text];        

NSUserDefaults *save = [NSUserDefaults standardUserDefaults];

[save setObject:array forKey:@"success" ];

[save synchronize];

-(void) viewDidLoad

NSUserDefaults *viewdata1 = [NSUserDefaults standardUserDefaults];

[viewdata1 objectForKey:@"success"];

[viewdata1 synchronize];

[tabledata reloadData];

数据保存在数组中后,应用程序再次运行后如何上传?我希望数据一次加载回表中。

4

2 回答 2

0

你应该这样做:

TSTableViewController.h:

@property(nonatomic, readwrite, retain) NSMutableArray* dataSource;

TSTableViewController.m:

- (id) init
{
    if ((self = [super init]))
    {
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(applicationDidEnterBackground:)
                                                     name: UIApplicationDidEnterBackgroundNotification
                                                   object: nil];
    }

    return self;
}

- (void) applicationDidEnterBackground: (NSNotification*) notification
{
    [[NSUserDefaults standardUserDefaults] setObject: self.dataSource
                                              forKey: @"success" ];
}

- (void) viewDidLoad
{
    [super viewDidLoad];

    NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey: @"success"];

    if (array)
    {
        self.dataSource = [NSMutableArray arrayWithArray: array];
    }
    else
    {
        self.dataSource = [[[NSMutableArray alloc] init] autorelease];
    }

    [tableView reloadData];

}
- (void) addDataToDataSource
{
    [self.dataSource addObject: textdata.text];
    [tabledata reloadData];
}

- (void) dealloc
{
    [dataSource release];
    dataSource = nil;

    [super dealloc];
}
于 2013-05-22T04:12:10.210 回答
0

The first step is to retrieve it from user defaults. The second step is not to drop it on the floor.

[viewdata1 objectForKey:@"success"];

This does one, but not the other: You retrieve it, but then you drop it on the floor.

You need to store the object as the value of a property (which means you will need to declare a property for that purpose), then, in your table view's data source, return the count of that array as your number of rows and objects in the array (or properties of those objects) as the row values.

Also, you shouldn't need to call synchronize, especially after retrieving the value.

于 2013-05-22T03:08:24.673 回答