在 uiviewTable ios 上传递 JSON 数据(目标 c)

Posted

技术标签:

【中文标题】在 uiviewTable ios 上传递 JSON 数据(目标 c)【英文标题】:Passing JSON data on uiviewTable ios (objective c) 【发布时间】:2016-08-04 12:21:40 【问题描述】:

有类似的问题,但我找不到任何适合我的解决方案。 我从链接中获得了所有数据为JSON,但我无法理解如何在 uitableview 上显示这些数据。它会显示在主页上。它有titleinfo。现在我只需要在主页上显示titleinfo

NSURL *url = [NSURL URLWithString:@"http://mantis.vu.edu.pk/fundme/public/api/v1/ideas"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    
        NSDictionary *responseDict = (NSDictionary *)JSON;
        ideasArrayList = [[NSMutableArray alloc] init];
       for (NSDictionary *innerObject in [responseDict objectForKey:@"data"])
           
              [ideasArrayList addObject:innerObject];
       if (ideasArrayList.count > 0) 
          NSDictionary *userObject = [ideasArrayList objectAtIndex:0];
          NSLog(@"Object and first index of array is %@",userObject);
       
       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
                                             

 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops something went wrong."
 message:[error localizedDescription]
 delegate:nil
 cancelButtonTitle:@"Ok"
 otherButtonTitles:nil];
 [alertView show];

  ];
  [jsonOperation start];

我正在使用AFNetworking 库。

【问题讨论】:

你能预览一下innerObject 添加有问题的userObjectinnerObject 的值。并添加您尝试过的cellforrowatindexpath 方法!! 使用 NSnotificationCenter 并将整个字典数据传递到您想要显示的位置并根据需要相应地解析数据。 您将在ideasArrayList 中获取所有响应数据。从该可变数组中,您可以将 idea_title 的值设为 [ideasArrayList Valueforkey:@"idea_title"]。 【参考方案1】:

如果你在 ViewController 中调用你的代码,首先你需要添加一个 dispatch_async 块来将你的数据移动到主线程并重新加载 tableview

-(void)getDataFromApi 

   NSURL *url = [NSURL URLWithString:@"http://mantis.vu.edu.pk/fundme/public/api/v1/ideas"];
   NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
   AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:urlRequest success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
    
        NSDictionary *responseDict = (NSDictionary *)JSON;
        ideasArrayList = [[NSMutableArray alloc] init];
       for (NSDictionary *innerObject in [responseDict objectForKey:@"data"])
           
              [ideasArrayList addObject:innerObject];
       if (ideasArrayList.count > 0) 
          NSDictionary *userObject = [ideasArrayList objectAtIndex:0];
          NSLog(@"Object and first index of array is %@",userObject);

          dispatch_async(dispatch_get_main_queue(), ^
              self.ideasArrayList = ideasArrayList;
              [self.tableView reloadData];
          );

       
       failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
           
          dispatch_async(dispatch_get_main_queue(), ^
               UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Oops something went wrong."
                message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
               [alertView show];
          );
  ];
  [jsonOperation start];

viewDidLoad方法中

- (void)viewDidLoad
   self.tableView.dataSource = self;

并实现UITableViewDatasource协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
    return self.ideasArrayList.count;


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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        //configure the cell here or create custom subclass
    

    NSDictionary *innerObject = self.ideasArrayList[indexPath.row];
    cell.textLabel.text = innerObject[@"title"];
    return cell;

【讨论】:

self.tableView.dataSource = self; 抛出错误property tableView not found on type HomeVC 如果你想在tableView中显示你的数据,你需要在你的ViewController中创建它 知道了!制作完成后。如何在其他控制器上显示它?我想在HomeVC 上显示它谢谢【参考方案2】:

您需要将需要在 tableview 中显示的值存储在数组中。因此,从您的输出 JSON 中检索这些值并将它们存储在一个数组中。然后在表格视图的数据源方法中遵循通常的方法。例如,在 numberOfRowsInSection 中返回 yourArray.count。我希望你明白这一点。

我希望你明白这一点。将值存储在数组中,然后从该数组中获取表。

【讨论】:

我已经做到了。检查ravi's answer。我只想在 uiTableView 上显示它。这是我做不到的。【参考方案3】:

我认为这对你有帮助。

首先您要添加.m 文件。

#import "ViewController.h"
#import "tblcellTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

    NSMutableArray *arrJSONDATA;

@property (weak, nonatomic) IBOutlet UITableView *tbl;

@end

并添加下面的代码viewDidLoad

arrJSONDATA = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://mantis.vu.edu.pk/fundme/public/api/v1/ideas"];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
    NSDictionary *dicData = [dic valueForKey:@"data"];
    for (NSDictionary *dic1 in dicData) 
        NSMutableArray *arr = [[NSMutableArray alloc] init];
        [arr addObject:[dic1 objectForKey:@"idea_title"]];
        [arr addObject:[dic1 objectForKey:@"idea_info"]];

        [arrJSONDATA addObject:arr];
    
    NSLog(@"%@",[arrJSONDATA description]);
    [_tbl reloadData];

titleinfo 创建标签出口。

#import <UIKit/UIKit.h>

@interface tblcellTableViewCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *lblTitle;
@property (weak, nonatomic) IBOutlet UILabel *lblInfo;
@end

然后创建tableView 委托方法。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    return [arrJSONDATA count];

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    return 1;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    tblcellTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    arr = [arrJSONDATA objectAtIndex:indexPath.section];
    cells.lblTitle.text = [arr objectAtIndex:0];
    cells.lblInfo.text = [arr objectAtIndex:1];

    return cells;

[Check the Screenshot.]

【讨论】:

我是通过这种方式获取数据的,但是如何在 uiViewTable 中查看这些数据? 把最后的sn-p代码放在哪里?并且没有 'tblCellTableViewCell.h' 如何处理它 You Prefer this link theappguruz.com/blog/customize-table-view-cells-uitableview-ios ... 创建 tableViewCell 文件然后选择 tableview 并选择单元格并导入类文件。

以上是关于在 uiviewTable ios 上传递 JSON 数据(目标 c)的主要内容,如果未能解决你的问题,请参考以下文章