我救两个标签的值通过NSUserDefaults的:检索NSUserDefaults的数据的TableView
- (IBAction) saveData
{
// Store the data
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:autore.text forKey:@"Author"];
[defaults setObject:testo.text forKey:@"Text"];
[defaults synchronize];
}
然后我尝试TOT在的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;
}
我会得到的结果类似图片:
我看到你这样做的方式只能有一个作者和一个文本。你打算有多个作者和文本? – dbrajkovic 2012-03-07 23:18:08
是的,有多个作者/文本 – Maurizio 2012-03-07 23:24:01
那么你需要创建一个具有两个属性的子类,例如作者和文本。然后制作这些对象的数组。然后你可以说'cell.textLabel.text = [dataArray objectAtIndex:indexPath.row] .author;''cell.detailTextLabel.text = [dataArray objectAtIndex:indexPath.row] .text;' – dbrajkovic 2012-03-07 23:30:26