Advertisement
julong

getdata from api JSON

Aug 12th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // cash
  4. //
  5. // Created by Apple on 8/13/2557 BE.
  6. // Copyright (c) 2557 Apple. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10. #import "DetailViewController.h"
  11. @interface ViewController ()
  12. {
  13. NSArray *tableData;
  14. NSArray *jsonObject;
  15. NSMutableArray *cashArrays;
  16. NSDictionary *temp;
  17. NSMutableArray *displayObject;
  18. NSArray *searchResults;
  19. NSMutableDictionary *testdict;
  20.  
  21. }
  22. @end
  23.  
  24. @implementation ViewController
  25. @synthesize tableView;
  26. @synthesize dict;
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. tableData = [NSArray arrayWithObjects:@"mycash",@"detail",nil];
  31. NSData *jsondata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"GIANT9"]];
  32.  
  33. jsonObject = [NSJSONSerialization JSONObjectWithData:jsondata options:NSJSONReadingMutableContainers error:nil];
  34. //NSString *string = [dict objectForKey:@"someKey"];
  35. cashArrays = [[NSMutableArray alloc] init];
  36. for (NSDictionary *dataDict in jsonObject) {
  37.  
  38. NSString *detail_data = [dataDict objectForKey:@"detail"];
  39. NSString *receipts_data = [dataDict objectForKey:@"receipts"];
  40. NSString *charge_data = [dataDict objectForKey:@"charge"];
  41. NSString *balance_data = [dataDict objectForKey:@"balance"];
  42. NSLog(@" detail name is : %@",detail_data);
  43. NSLog(@" receipts name is : %@",receipts_data);
  44. NSLog(@" charge name is : %@",charge_data);
  45. NSLog(@" balance is : %@",balance_data);
  46. dict = [NSDictionary dictionaryWithObjectsAndKeys:
  47. detail_data,@"detail",
  48. receipts_data,@"receipts",
  49. charge_data,@"charge",
  50. balance_data,@"balance",
  51. nil];
  52. [cashArrays addObject:dict];
  53.  
  54. }
  55. //NSLog(@"object index 0 %@",[cashArrays objectAtIndex:0]);
  56. //testdict = [[NSMutableDictionary alloc]init];
  57. //testdict = [[cashArrays objectAtIndex:0]valueForKey:@"balance"];
  58. //NSLog(@"testdict detail 0 is : %@",testdict);
  59. displayObject =[[NSMutableArray alloc] initWithArray:cashArrays];
  60. // Do any additional setup after loading the view, typically from a nib.
  61. }
  62.  
  63. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  64.  
  65. return displayObject.count;
  66.  
  67. }
  68.  
  69. #pragma mark Tableview
  70. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  71. static NSString *CelleTdentifier = @"mycash";
  72. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CelleTdentifier];
  73. if(cell == nil){
  74. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CelleTdentifier];
  75. }
  76.  
  77.  
  78. temp = [displayObject objectAtIndex:indexPath.row];
  79.  
  80. NSString *cellValue = [temp objectForKey:@"detail"]; //cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
  81. cell.textLabel.text = cellValue;
  82. cell.imageView.image = [UIImage imageNamed:@"pictureProfilessm2014-07-15.09_29_19-53c4f46f5c033-941406_459727600783769_62486325_n.jpg"];
  83. return cell;
  84. }
  85.  
  86.  
  87. #pragma mark uisearchbar
  88. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  89. {
  90. if([searchString length] == 0)
  91. {
  92. [displayObject removeAllObjects];
  93. [displayObject addObjectsFromArray:cashArrays];
  94. }
  95. else
  96. {
  97. [displayObject removeAllObjects];
  98. for(NSDictionary *tmpDict in cashArrays)
  99. {
  100. NSString *val = [tmpDict objectForKey:@"detail"];
  101. NSRange r = [val rangeOfString:searchString options:NSCaseInsensitiveSearch];
  102. if(r.location != NSNotFound)
  103. {
  104. [displayObject addObject:tmpDict];
  105. }
  106. }
  107. }
  108. return YES;
  109. }
  110.  
  111. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
  112. searchBar.text=@"";
  113. [searchBar setShowsCancelButton:NO animated:YES];
  114. [searchBar resignFirstResponder];
  115. }
  116. #pragma mark heightForRowAtIndexPath
  117. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. return 71;
  120. }
  121.  
  122. #pragma mark send parameter
  123. -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  124. if ([segue.identifier isEqualToString:@"showdetail"]) {
  125.  
  126. NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
  127.  
  128. temp = [displayObject objectAtIndex:indexPath.row];
  129.  
  130. NSString *cellValue = [temp objectForKey:@"detail"];
  131.  
  132. DetailViewController *destViewController = segue.destinationViewController;
  133. destViewController.detail = cellValue;
  134. }
  135. }
  136.  
  137.  
  138. - (void)didReceiveMemoryWarning
  139. {
  140. [super didReceiveMemoryWarning];
  141. // Dispose of any resources that can be recreated.
  142. }
  143.  
  144.  
  145. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement