Ios 将数据从嵌套的NSUSERDEFAULTS加载到UITABLEVIEW
我有一个DetailViewController,如果用户需要,它会将一些数据保存到NSUserDefaults。这就像是一个最受欢迎的列表。Hier是保存的默认值的结构:Ios 将数据从嵌套的NSUSERDEFAULTS加载到UITABLEVIEW,ios,objective-c,uitableview,nsdictionary,nsuserdefaults,Ios,Objective C,Uitableview,Nsdictionary,Nsuserdefaults,我有一个DetailViewController,如果用户需要,它会将一些数据保存到NSUserDefaults。这就像是一个最受欢迎的列表。Hier是保存的默认值的结构: List = { 0002 = ( { Picture = "link to picture.jpg"; Place = "London";
List = {
0002 = (
{
Picture = "link to picture.jpg";
Place = "London";
Title = "Flat with Balcony";
}
);
0003 = (
{
Picture = "link to picture.jpg";
Place = "Duesseldorf";
Title = "Roof Garden"
}
);
};
在视图中加载:
- (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中使用它们?尝试使用
[\u myDict objectForKey:
[\u keysArray objectAtIndex:indexath.row]]。这将为您提供其中一个键的值,这是一个包含3个键(Title、place和picture)的字典数组
试试这个代码
NSArray *dataArray = [_myDict objectForKey:_keysArray[indexPath.row]];
NSDictionary *data = dataArray[0];
cell.textLabel.text = [data objectForKey@"Title"];
_myDict是一个列表字典,包含0002和0003处的两个数组,因此您需要首先提取数组,然后获取第一个对象,即您的数据。现在您可以访问对象的所有数据。不起作用。它给出:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFArray IsequaltString:]:未识别的选择器发送到实例0x16e54300'您希望为您的单元格标签分配什么值?对于标题列表->0002->标题:“带阳台的平面”对于字幕列表->0002->位置:“伦敦”等等。cell.textLabel.text=[[u keysArray objectAtIndex:[indexath行]]将0002和0003作为标题。但我希望在标题、位置和图片方面更深入一层。感谢您的帮助,但由于未捕获的异常“NSInvalidArgumentException”,它会终止应用程序,原因:'-[\uu NSCFArray objectForKey::]:未识别的选择器发送到实例0x16e284e0'嘿,谢谢,但键值是NSDictionary。它不能编译。
NSArray *dataArray = [_myDict objectForKey:_keysArray[indexPath.row]];
NSDictionary *data = dataArray[0];
cell.textLabel.text = [data objectForKey@"Title"];