2016-08-04 3 views
0

同様の質問がありますが、私に合った解決策が見つかりませんでした。 私はリンクからのすべてのデータをJSONとしていますが、そのデータをuitableviewにどのように表示できるのか理解できません。ホームページに掲載される予定です。それはtitle,infoです。今のところ、ホームページにはtitleinfoが必要です。uiviewTable ios(目的c)でJSONデータを渡す

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ライブラリを使用しています。

+0

「innerObject」 –

+0

をプレビューすると、問題の 'userObject'または' innerObject'の値を追加できます。 'cellforrowatindexpath'メソッドを追加してみてください! – Lion

+0

NSnotificationCenterを使用して、必要に応じてデータを表示して解析する場所に辞書データ全体を渡します。 –

答えて

0

あなたはViewControllerをであなたのコードを呼び出す場合は、最初にあなたがメインスレッドにデータを移動し、viewDidLoad方法

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

そしてUITableViewDatasourceを実装するにはテーブルビュー

-(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]; 
} 

をリロードするためのdispatch_asyncブロックを追加必要プロトコル方法

- (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; 
} 
+0

'self.tableView.dataSource = self;' throws error 'プロパティテーブルビューがタイプ' ** HomeVC ** –

+0

に見つかりません。あなたのデータをtableViewに表示するには、ViewControllerでデータを作成する必要があります – iSashok

+0

!それを作った後。どのように私は他のcontorllerでそれを表示できますか?私は 'HomeVC'にそれを示したいと思います。 –

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.]

+0

私はこのようにデータを取得しましたが、どのようにしてこのデータをuiViewTableで見ることができますか? –

+0

タイトルと情報をtableviewに表示したいですか? –

+0

はい、私はテーブルビュー –

0

あなたは、アレイ内のテーブルビューで表示する必要がある値を格納する必要があります。その値を出力JSONから取得し、配列に格納します。次に、テーブルビューのデータソースメソッドでは、通常通りに続けます。たとえば、numberOfRowsInSectionでyourArray.countを返します。私はあなたがポイントを得ることを願っています。

私はあなたがポイントを得ることを願っています。配列に値を格納し、その配列からテーブルをフェッチします。

+0

私はすでにそれをしています。 'raviの答え'をチェックしてください。私はuiTableViewに表示したいだけです。私はそれをすることができません。 –

関連する問題