Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "SPCategoriesViewController.h"
- @interface SPCategoriesViewController () {
- NSArray *recipeImages;
- NSString *parentID;
- }
- @end
- @implementation SPCategoriesViewController
- @synthesize segmentedControl;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"mainlogo.png"] forBarMetrics:UIBarMetricsDefault];
- [self setNeedsStatusBarAppearanceUpdate];
- [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
- [self.collectionView registerClass:[UICollectionViewCell class]forCellWithReuseIdentifier:@"Cell"];
- self.collectionView.delegate = self;
- self.collectionView.dataSource=self;
- NSData *allCategoriesData = [[NSData alloc] initWithContentsOfURL:
- [NSURL URLWithString:@"http://example.com/getData"]];
- NSError *error;
- self.allCategories = [NSJSONSerialization
- JSONObjectWithData:allCategoriesData
- options:kNilOptions
- error:&error];
- [self.collectionView reloadData];
- }
- //set collection view margin
- - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
- return UIEdgeInsetsMake(8, 8, 8, 8);
- }
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return [self.allCategories count];
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
- UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
- UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
- NSDictionary *category = [self.allCategories objectAtIndex:[indexPath row]];
- UILabel *titleLabel = (UILabel *)[cell viewWithTag:200];
- [titleLabel setText:[category objectForKey:@"name"]];
- dispatch_async(dispatch_get_global_queue(0,0), ^{
- NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[category objectForKey:@"image"]]];
- if ( data == nil )
- return;
- dispatch_async(dispatch_get_main_queue(), ^{
- recipeImageView.image = [UIImage imageWithData: data];
- });
- });
- cell.layer.cornerRadius = 3.0;
- cell.layer.masksToBounds = NO;
- cell.layer.shadowColor = [UIColor blackColor].CGColor;
- cell.layer.shadowOffset = CGSizeMake(2, 2);
- cell.layer.shadowOpacity = 0.5;
- cell.layer.shadowRadius = 1.2;
- cell.clipsToBounds = NO;
- return cell;
- }
- - (IBAction)segmentedControlAction:(id)sender {
- switch (segmentedControl.selectedSegmentIndex) {
- case 0:
- parentID = @"1";
- break;
- case 1:
- parentID = @"12";
- break;
- case 2:
- parentID = @"18";
- break;
- }
- NSLog(@"%@", parentID);
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement