1 个 JSON 键,具有多个值,每个值分隔成 uitableview 单元格

Posted

技术标签:

【中文标题】1 个 JSON 键,具有多个值,每个值分隔成 uitableview 单元格【英文标题】:1 JSON key with multiple values each separated into uitableview cells 【发布时间】:2016-07-15 03:39:24 【问题描述】:

构建一个 ios 应用程序,该应用程序从本地 JSON 数组中解析关键“成分”,然后在表格视图中显示“成分”值。问题是每个成分键都有多个值。每个值都需要分成自己的表格视图单元格,我不知道如何完成。我的知识非常有限,我只能将组合成分值显示到表格视图单元格中。

如何分隔这些值,以便成分中的每个值都有自己的表格视图单元格?

这里只是 JSON 数组中的 2 个数据示例:


  "locations": [


"name" : "Drop Biscuits and Sausage Gravy", "ingredients" :     "Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Butermilk\n SAUSAGE GRAVY\n1 pound Breakfast Sausage, Hot Or Mild\n1/3 cup All-purpose Flour\n4 cups Whole Milk\n1/2 teaspoon Seasoned Salt\n2 teaspoons Black Pepper, More To Taste", "cookTime" : "PT30M",

"name" : "Hot Roast Beef Sandwiches", "ingredients" : "12 whole Dinner Rolls Or Small Sandwich Buns (I Used Whole Wheat)\n1 pound Thinly Shaved Roast Beef Or Ham (or Both!)\n1 pound Cheese (Provolone, Swiss, Mozzarella, Even Cheez Whiz!)\n1/4 cup Mayonnaise\n3 Tablespoons Grated Onion (or 1 Tbsp Dried Onion Flakes))\n1 Tablespoon Poppy Seeds\n1 Tablespoon Spicy Mustard\n1 Tablespoon Horseradish Mayo Or Straight Prepared Horseradish\n Dash Of Worcestershire, "cookTime" : "PT20M"
  ]

这是表格视图代码:

#import "FilterViewController.h"
#import "LocationsViewController.h"
#import "Location.h"
#import "JSONLoader.h"

@implementation FilterViewController 
NSArray *_locations;






- (void)viewDidLoad 
[super viewDidLoad];

// Create a new JSONLoader with a local file URL
JSONLoader *jsonLoader = [[JSONLoader alloc] init];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"locations" withExtension:@"json"];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
    _locations = [jsonLoader locationsFromJSONFile:url];

    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];


);



- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
LocationsViewController *vc = segue.destinationViewController;
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
vc.location = [_locations objectAtIndex:indexPath.row];


#pragma mark - Table View Controller Methods

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FilterCell"];

Location *location = [_locations objectAtIndex:indexPath.row

cell.textLabel.text = location.ingredients;

cell.imageView.image = [UIImage imageNamed:@"ingredientsicon3232.png"];

return cell;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
return [_locations count];


@end

这是 Location.h 文件

#import <Foundation/Foundation.h>

@interface Location : NSObject

- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary;



@property (readonly) NSString *name;
@property (readonly) NSString *ingredients;
@property (readonly) NSString *cookTime;



@end

和 Location.m 文件

#import "Location.h"

@implementation Location

// Init the object with information from a dictionary
- (id)initWithJSONDictionary:(NSDictionary *)jsonDictionary 
    if(self = [self init]) 
        // Assign all properties with keyed values from the dictionary


        _name = [jsonDictionary objectForKey:@"name"];
        _ingredients = [jsonDictionary objectForKey:@"ingredients"];
        _cookTime = [jsonDictionary objectForKey:@"cookTime"];

    

    return self;


@end

【问题讨论】:

Location 是如何声明的?基本上,您需要一个数组 ingredients 和方法 componentsSeparatedByString 将值分隔到一个数组中。然后在表格视图中使用Location 对象作为sectioningredients 作为rows json 数据无效,所以我正在编辑它 感谢您的回复。我无法让这些解决方案发挥作用,因为它们都会抛出错误......太多无法列出。 @vadian 我正在添加位置代码。您能否在代码中提供您的建议示例?这将不胜感激。我还在学习。 【参考方案1】:

试试这个:--

 NSString *str_Calories_List = [[responseObject valueForKeyPath:@"location.name"] componentsJoinedByString:@", "];

【讨论】:

【参考方案2】:

我已经尝试过使用您的 json 数据的方法

NSDictionary *setObject = @
        @"name": @"Drop Biscuits and Sausage Gravy",
        @"ingredients": @"Biscuits\n3 cups All-purpose Flour\n2 Tablespoons Baking Powder\n1/2 teaspoon Salt\n1-1/2 stick (3/4 Cup) Cold Butter, Cut Into Pieces\n1-1/4 cup Butermilk\n SAUSAGE GRAVY\n1 pound Breakfast Sausage, Hot Or Mild\n1/3 cup All-purpose Flour\n4 cups Whole Milk\n1/2 teaspoon Seasoned Salt\n2 teaspoons Black Pepper, More To Taste",
        @"cookTime": @"PT30M"
        ;


    NSArray *getArrayRespose =[NSArray arrayWithObjects:setObject,setObject, nil];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:getArrayRespose forKey:@"locations"];
    NSLog(@"%@",dic);

    //I am tested to separate the string like below  

    NSArray *getObject = [dic objectForKey:@"locations"];
    NSDictionary *getFirstObject = [getObject objectAtIndex:0];
    NSString *name = [getFirstObject objectForKey:@"name"];
    NSString *ingred = [getFirstObject objectForKey:@"ingredients"];
    NSString *cook = [getFirstObject objectForKey:@"cookTime"];



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return  1;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
   NSArray *getObject = [dic objectForKey:@"locations"];
    return [getObject count];


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   //  here you create the tableviewCell
    NSArray *getObject = [dic objectForKey:@"locations"];
    NSDictionary *getFirstObject = [getObject objectAtIndex:indexPath.row];
    NSString *name = [getFirstObject objectForKey:@"name"];
    NSString *ingred = [getFirstObject objectForKey:@"ingredients"];
    NSString *cook = [getFirstObject objectForKey:@"cookTime"];

    // can you use the separated string in your cell

    cell.titleLabel.text = name;
   

【讨论】:

感谢您的回复,但是您是否有一个示例可以像我当前的代码一样从我的本地 json 文件中读取?有数以千计的这些食谱,并且像您一样将它们全部输入到 tableview 控制器中是不可行的。 参考这个链接。***.com/questions/22528800/…分页器​​用来加载1000条数据。可以用同一个字典来加载tableview中的数据

以上是关于1 个 JSON 键,具有多个值,每个值分隔成 uitableview 单元格的主要内容,如果未能解决你的问题,请参考以下文章

从每个键具有多个值的字典中将数据写入 csv

创建和查找每个值具有多个键的 2D 字典

Postgresql 更新 JSON 列保留一些键值并将附加键值添加为空

每个键有多个值的列表到字典转换?

JSON对象与字符串相互转化ObjectMapper

一种在 JSON 中发送多个“键”:“值”对的方法