【问题标题】:Loading data from nested NSUSERDEFAULTS into UITABLEVIEW将数据从嵌套的 NSUSERDEFAULTS 加载到 UITABLEVIEW
【发布时间】: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


    【解决方案1】:

    试试这个代码

    NSArray *dataArray = [_myDict objectForKey:_keysArray[indexPath.row]];
    NSDictionary *data = dataArray[0];
    cell.textLabel.text = [data objectForKey@"Title"];
    

    _myDict 是 List 字典,其中包含 0002 和 0003 处的两个数组,因此您需要先提取数组,然后获取第一个对象,即您的数据。现在您可以访问对象的所有数据了。

    【讨论】:

      【解决方案2】:

      尝试使用[_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. 
      

      【讨论】:

      • 不起作用。它给出:由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFArray isEqualToString:]: unrecognized selector sent to instance 0x16e54300'
      • 您要为单元格标签分配什么值?
      • 对于标题列表->0002->标题:“带阳台的公寓”对于字幕列表->0002->地点:“伦敦”等等。 cell.textLabel.text = [_keysArray objectAtIndex:[indexPath row]] 给出 0002 和 0003 作为标题。但我希望标题、地点和图片的覆盖范围更深一层。
      • 感谢您的帮助,但由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x16e284e0'
      • 嘿,谢谢,但 keyValue 是 NSDictionary。它无法编译。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多