Advertisement
Guest User

SPCategoriesViewController.m

a guest
Aug 13th, 2014
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "SPCategoriesViewController.h"
  2.  
  3. @interface SPCategoriesViewController () {
  4.     NSArray *recipeImages;
  5.     NSString *parentID;
  6. }
  7.  
  8. @end
  9.  
  10. @implementation SPCategoriesViewController
  11.  
  12. @synthesize segmentedControl;
  13.  
  14. - (void)viewDidLoad
  15. {
  16.     [super viewDidLoad];
  17.    
  18.     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"mainlogo.png"] forBarMetrics:UIBarMetricsDefault];
  19.     [self setNeedsStatusBarAppearanceUpdate];
  20.     [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
  21.    
  22.     [self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];
  23.     self.collectionView.delegate = self;
  24.     self.collectionView.dataSource=self;
  25.    
  26.     NSData *allCategoriesData = [[NSData alloc] initWithContentsOfURL:
  27.                                  [NSURL URLWithString:@"http://example.com/getData"]];
  28.     NSError *error;
  29.     self.allCategories = [NSJSONSerialization
  30.                           JSONObjectWithData:allCategoriesData
  31.                           options:kNilOptions
  32.                           error:&error];
  33.     [self.collectionView reloadData];
  34. }
  35.  
  36.  
  37. //set collection view margin
  38. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  39.     return UIEdgeInsetsMake(8, 8, 8, 8);
  40. }
  41.  
  42. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  43.     return [self.allCategories count];
  44. }
  45.  
  46. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  47.    
  48.     UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
  49.     UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
  50.     NSDictionary *category = [self.allCategories objectAtIndex:[indexPath row]];
  51.    
  52.     UILabel *titleLabel = (UILabel *)[cell viewWithTag:200];
  53.     [titleLabel setText:[category objectForKey:@"name"]];
  54.    
  55.     dispatch_async(dispatch_get_global_queue(0,0), ^{
  56.         NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[category objectForKey:@"image"]]];
  57.         if ( data == nil )
  58.             return;
  59.         dispatch_async(dispatch_get_main_queue(), ^{
  60.             recipeImageView.image = [UIImage imageWithData: data];
  61.         });
  62.     });
  63.    
  64.     cell.layer.cornerRadius = 3.0;
  65.     cell.layer.masksToBounds = NO;
  66.     cell.layer.shadowColor = [UIColor blackColor].CGColor;
  67.     cell.layer.shadowOffset = CGSizeMake(2, 2);
  68.     cell.layer.shadowOpacity = 0.5;
  69.     cell.layer.shadowRadius = 1.2;
  70.     cell.clipsToBounds = NO;
  71.    
  72.     return cell;
  73. }
  74.  
  75. - (IBAction)segmentedControlAction:(id)sender {
  76.    
  77.     switch (segmentedControl.selectedSegmentIndex) {
  78.         case 0:
  79.             parentID = @"1";
  80.             break;
  81.         case 1:
  82.             parentID = @"12";
  83.             break;
  84.         case 2:
  85.             parentID = @"18";
  86.             break;
  87.     }
  88.     NSLog(@"%@", parentID);
  89.  
  90. }
  91. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement