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

时间:2016-08-04 12:21:40

标签: ios objective-c iphone json uitableview

有类似的问题,但我找不到适合我的任何解决方案。 我从链接中获取了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库。

3 个答案:

答案 0 :(得分:0)

如果您在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;
}

答案 1 :(得分:0)

我认为这对你有所帮助。

首先,您要添加.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.]

答案 2 :(得分:0)

您需要将在tableview中显示所需的值存储在数组中。因此,从输出JSON中检索这些值并将它们存储在数组中。然后在表视图的数据源方法中按照惯例进行操作。例如,在numberOfRowsInSection中返回yourArray.count。我希望你明白这一点。

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