我救兩個標籤的值通過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