简体   繁体   English

从NSUserDefaults检索数据到TableView

[英]retrieve data from NSUserDefaults to TableView

I save values of two labels through NSUserDefaults : 我通过NSUserDefaults保存两个标签的值:

- (IBAction) saveData
{
    // Store the data
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:autore.text forKey:@"Author"];
    [defaults setObject:testo.text forKey:@"Text"];
    [defaults synchronize];    
}

Then i try tot retrieve those values in a tableView : 然后我尝试在tableView中检索这些值:

// NSArray
@synthesize dataArray;


- (void)viewDidLoad {
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
self.dataArray = [NSArray arrayWithObjects:[prefs objectForKey:@"Author"], [prefs objectForKey:@"Text"],nil];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// There is only one section.
return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of time zone names.
return [dataArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 ..............
 ..............

 NSString *string = [dataArray objectAtIndex:indexPath.row];

 // Authors
 cell.textLabel.text = string;
 // Text
 cell.detailTextLabel.text = string;

 return cell;
}

I would obtain result as like picture: 我将获得如图所示的结果: 数字

I understand your problem, but first of all I need to know what you are saving. 我了解您的问题,但首先我需要知道您要节省什么。 Are U saving just one string to the defaults or more, because if you are saving more I would recommend to add all this files first of all to array and then save it to the defaults. U是否仅将一个字符串保存为默认值或更多,因为如果要保存更多,我建议首先将所有这些文件添加到数组,然后将其保存为默认值。

So NSMutableArray * myArray = [[[NSMutableArray alloc] init] autorelease]; [myArray addObject:myfirstauthor]; [myArray addObject:mysecondauthor]; [myArray addObject:mythirdauthor]; .... 因此, NSMutableArray * myArray = [[[NSMutableArray alloc] init] autorelease]; [myArray addObject:myfirstauthor]; [myArray addObject:mysecondauthor]; [myArray addObject:mythirdauthor]; .... NSMutableArray * myArray = [[[NSMutableArray alloc] init] autorelease]; [myArray addObject:myfirstauthor]; [myArray addObject:mysecondauthor]; [myArray addObject:mythirdauthor]; .... NSMutableArray * myArray = [[[NSMutableArray alloc] init] autorelease]; [myArray addObject:myfirstauthor]; [myArray addObject:mysecondauthor]; [myArray addObject:mythirdauthor]; .... the same with quotations.... NSMutableArray * myArray = [[[NSMutableArray alloc] init] autorelease]; [myArray addObject:myfirstauthor]; [myArray addObject:mysecondauthor]; [myArray addObject:mythirdauthor]; ....与引号相同...

and then i would save arrays to the NSUserDefaults as you correctly did. 然后我将数组正确地保存到NSUserDefaults中。 On loading for the next time, take these arrays from defaults and retrieve information from there. 下次加载时,请从默认值中获取这些数组,并从中获取信息。

Because in this case U're saving just one long string. 因为在这种情况下,U仅保存了一个长字符串。

One more notice, but I am not sure about this, maybe everything saved to the defaults should be type of NSDATA, but i am not sure, take a look on programming guide.=) 再通知一遍,但是我不确定,也许保存到默认值的所有内容都应该是NSDATA类型,但是我不确定,请看一下编程指南。

Happy coding=) 编码愉快=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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