【发布时间】:2015-02-01 18:47:27
【问题描述】:
如果用户需要,我有一个 DetailViewController 将一些数据保存到 NSUserDefaults。这就像一种最喜欢的列表。 Hier 是保存的 Defaults 的结构:
List = {
0002 = (
{
Picture = "link to picture.jpg";
Place = "London";
Title = "Flat with Balcony";
}
);
0003 = (
{
Picture = "link to picture.jpg";
Place = "Duesseldorf";
Title = "Roof Garden"
}
);
};
在 viewDidLoad 中:
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
// Save to the Dictionary
_myDict = [[userdefaults dictionaryRepresentation] objectForKey:@"List"];
NSLog(@"MYDICT : %@", _myDict);
// save only the keys which are 0002 and 0003 etc..
_keysArray = [_myDict allKeys];
NSLog(@"keysArray : %@", _keysArray);
// Print out the elements in keysArray
for(NSString *key in _keysArray){
NSLog(@"key : %@", key);
}
}
在
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_keysArray count]; // gives 2
}
终于来了
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"favoritesCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// Configure the cell...
// this doesn't work gives only nothing ( not error) It is just empty
cell.textLabel.text = [_myDict valueForKeyPath:[NSString stringWithFormat:@"List.%@",_keysArray[indexPath.row]]];
//cell.textLabel.text = [_myDict valueForKeyPath:@"List"]; // this gives also nothing just empty
//How is it possible to reach a value of a key in nested NSUserdefaults
return cell;
}
如何在嵌套的 Nsuserdefaults 中获取一个键值并在 UITableView 中使用它们?
【问题讨论】:
标签: ios objective-c uitableview nsdictionary nsuserdefaults