【问题标题】:Populate UITableView with NSDictionary contained parsed JSON data from a mysql db使用 NSDictionary 填充 UITableView,其中包含来自 mysql db 的已解析 JSON 数据
【发布时间】:2013-01-17 22:15:29
【问题描述】:

非常熟悉 Android 编程,但对 iOS(和 Objective-C)非常陌生。

我在我的应用程序中调用了一个远程 php 文件,并且(我相信)根据我的 NSLOG 结果成功解析了 JSON 结果。示例:

2013-01-17 14:24:30.611 JSON TESTING 4[1309:1b03] Deserialized JSON Dictionary = {
products =     (
            {
        BF = "";
        EN = "2342";
        Measure = ft;
        Name = "Brian";
        "Name_id" = 1;
        Home = "New York";
        "DB_id" = 1;
    },
            {
        BF = "";
        EN = "2123";
        Measure = ft;
        Name = "Rex";
        "Name_id" = 3;
        Home = "New York";
        "DB_id" = 5;
    }
);
success = 1;

}

我的问题在于如何将这些信息填充到表格视图中。我可以定制一个原型单元,但我该去哪里呢?

编辑:

这是我的视图设置代码:

#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return productArray.count;
    NSLog(@"Number of arrays %u", productArray.count);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) 
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }


    NSDictionary *productDictionary = [productArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [productDictionary objectForKey:@"BF"];

    return cell;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self launchTest];
}

还有我的 .h 文件

@interface tpbaMasterViewController : UITableViewController 
{
    NSDictionary *lists;
    NSArray *productArray;
}

- (void) launchTest;

@property (strong, nonatomic) IBOutlet UITableView *tableView;



@end

【问题讨论】:

  • 列表 = (NSDictionary *)jsonObject; productArray = [列出 objectForKey:@"products"];

标签: ios objective-c json uitableview nsdictionary


【解决方案1】:

您可以使用objectForKey 方法访问NSDictionary 中的对象。例如,要获取字典中产品的NSArray

NSArray *productArray = [myDictionary objectForKey:@"products"];

现在你有了一个包含两个字典对象的数组。对于各种UITableViewDataSource 方法,您可以查询数组。举几个例子:

对于– tableView:numberOfRowsInSection:,返回数组中的对象个数:

`return productArray.count;`

对于tableView:cellForRowAtIndexPath:

NSDictionary *productDictionary = [productArray objectAtIndex:indexPath.row];
    myCell.bfLabel.text = [productDictionary objectForKey:@"BF"];
    myCell.enLabel.text = [productDictionary objectForKey:@"EN"];
   //  continue doing the same for the other product information

如下所示在 .m 文件中声明 productArray 使其在您的视图控制器中可见(假设 productDictionary 是一个属性:

@interface MyCollectionViewController () {
    NSArray *productArray;
}
@end
...
@implementation MyCollectionViewController

    -(void)viewDidLoad{
        [super viewDidLoad];

        productArray = [self.myDictionary objectForKey:@"products"];
    }
...
@end

【讨论】:

  • 使用新的 Objective-C 语法,您可以将 [myDictionary objectForKey:@"products"] 写成 myDictionary[@"products"]
  • 谢谢。我在哪里声明我的 NSArray *productArray 以在我的 UITableViewDataSource 方法中使用它?
  • @ADK - 您可以在 .m 文件的接口块中声明它。更新了我的答案以显示这一点。
  • 感谢您的持续帮助,不胜感激。我在上面编辑了我的问题以显示我的支持代码。我的程序仍在运行,但 TableView 本身实际上没有填充任何内容(日志仍在正确吐出解析的 JSON,我没有粘贴该方法,因为我认为它工作正常)。
  • 几件事...我看不到productArray 的设置位置,- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 中的代码行需要颠倒。
猜你喜欢
  • 1970-01-01
  • 2014-09-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-09
  • 1970-01-01
  • 2015-09-27
  • 2020-08-07
  • 2014-04-07
相关资源
最近更新 更多