将数据从嵌套的 NSUSERDEFAULTS 加载到 UITABLEVIEW
Loading data from nested NSUSERDEFAULTS into UITABLEVIEW
我有一个 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 中使用它们?
尝试使用 [_myDict objectForKey:
[_keysArray objectAtIndex:indexPath.row]]。这将为您提供其中一个键的值,该键是具有 3 个键(标题、地点和图片)的字典数组。
NSArray * keyValue = [_myDict objectForKey:[_keysArray objectAtIndex:indexPath.row]]:
cell.textLabel.text = [keyValue[0] objectForKey:@"Title"];
//will set the cell text to the Title value.
试试这个代码
NSArray *dataArray = [_myDict objectForKey:_keysArray[indexPath.row]];
NSDictionary *data = dataArray[0];
cell.textLabel.text = [data objectForKey@"Title"];
_myDict 是包含 0002 和 0003 两个数组的 List 字典,因此您需要先提取数组,然后获取作为数据的第一个对象。现在您可以访问对象的所有数据了。
我有一个 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 中使用它们?
尝试使用 [_myDict objectForKey:
[_keysArray objectAtIndex:indexPath.row]]。这将为您提供其中一个键的值,该键是具有 3 个键(标题、地点和图片)的字典数组。
NSArray * keyValue = [_myDict objectForKey:[_keysArray objectAtIndex:indexPath.row]]:
cell.textLabel.text = [keyValue[0] objectForKey:@"Title"];
//will set the cell text to the Title value.
试试这个代码
NSArray *dataArray = [_myDict objectForKey:_keysArray[indexPath.row]];
NSDictionary *data = dataArray[0];
cell.textLabel.text = [data objectForKey@"Title"];
_myDict 是包含 0002 和 0003 两个数组的 List 字典,因此您需要先提取数组,然后获取作为数据的第一个对象。现在您可以访问对象的所有数据了。