Json Parsing Sample Project

ViewController.h

#import <UIKit/UIKit.h>

#import “VendorListParser.h”

#import “VendorListViewController.h”

@interface ViewController : UIViewController

– (IBAction)btn1:(id)sender;

@end

ViewController.m

#import “ViewController.h”

@interface ViewController ()

@end

@implementation ViewController

– (void)viewDidLoad {

    [super viewDidLoad];

    

    

    // Do any additional setup after loading the view from its nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

/*

#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

– (IBAction)btn1:(id)sender {

    

    

    VendorListParser *aListParser = [[VendorListParser alloc] init];

    

    [aListParser getVendorsList:^(NSMutableArray *vendorsList) {

        VendorListViewController *vndrlst=[[VendorListViewController alloc]initWithNibName:@”VendorListViewController” bundle:nil];

        vndrlst.vendorsList = vendorsList;

        [self.navigationController pushViewController:vndrlst animated:YES];

    } withFailureBlock:^(NSString *errorMessage) {

        UIAlertController *anAlertController = [UIAlertController alertControllerWithTitle:@”” message:errorMessage preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *anAction = [UIAlertAction actionWithTitle:@”OK” style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            

        }];

        [anAlertController addAction:anAction];

        [self presentViewController:anAlertController animated:YES completion:^{

            

        }];

    }];

}

@end

VendorListViewController.h

#import <UIKit/UIKit.h>

#import “Vendor.h”

@interface VendorListViewController : UIViewController<UICollectionViewDataSource,UICollectionViewDelegate>

{

    NSMutableArray *_myCollectionArray;

   

}

@property (strong, nonatomic) IBOutlet UICollectionView *collectionView1;

@property (nonatomic, retain) NSMutableArray *vendorsList;

//@property (nonatomic, strong) NSArray *dataArray;

– (IBAction)clsbtnactn:(id)sender;

@end

VendorListViewController.m

#import “VendorListViewController.h”

#import “NibCell.h”

@interface VendorListViewController ()

@end

@implementation VendorListViewController

@synthesize collectionView1;

– (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@”bg image”]];

    collectionView1.backgroundColor = [UIColor colorWithWhite:0 alpha:0];

    

    self.navigationController.navigationBarHidden=YES;

//    NSMutableArray *firstSection = [[NSMutableArray alloc] init]; NSMutableArray *secondSection = [[NSMutableArray alloc] init];

//    for (int i=0; i<50; i++) {

//        [firstSection addObject:[NSString stringWithFormat:@”Cell %d”, i]];

//        [secondSection addObject:[NSString stringWithFormat:@”item %d”, i]];

//    }

//    self.dataArray = [[NSArray alloc] initWithObjects:firstSection, secondSection, nil];

//    

    

    UINib *cellNib = [UINib nibWithNibName:@”NibCell” bundle:nil];

    [self.collectionView1 registerNib:cellNib forCellWithReuseIdentifier:@”cvCell”];

    

    

    

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];

    [flowLayout setItemSize:CGSizeMake(160, 270)];

    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    

    [self.collectionView1 setCollectionViewLayout:flowLayout];

  //  collectionView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@”bg image”]];

    

    // Do any additional setup after loading the view from its nib.

}

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

   // return [self.dataArray count];

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

  //  NSMutableArray *sectionArray = [self.dataArray objectAtIndex:section];

   // return [sectionArray count];

    return [self.vendorsList count];

}

-(NibCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    

//    NSMutableArray *data = [self.dataArray objectAtIndex:indexPath.section];

    

//    NSString *cellData = [data objectAtIndex:0];

    

    static NSString *cellIdentifier = @”cvCell”;

    

    NibCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    NSLog(@”%ld”,(long)indexPath.row);

    cell.callimgvw.tag = (long)indexPath.item;

    Vendor *aVendor = [self.vendorsList objectAtIndex:indexPath.item];

    cell.lbl2.text = aVendor.vendorName;

    cell.lbl3.text = [self convertHTML:aVendor.vendorDesc];

    cell.lbl1.text = [NSString stringWithFormat:@”$%0.2f”,aVendor.price];

    

    NSLog(@”%@”,[self convertHTML:aVendor.vendorDesc]);

    

    if (aVendor.vendorLogo.length > 0) {

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            NSURL *aUrl = [NSURL URLWithString:aVendor.vendorLogo];

           UIImage *aImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:aUrl]];

            dispatch_async(dispatch_get_main_queue(), ^{

                cell.imgvw.image = aImage;

            });

        });

    }

    

    return cell;

    

}

#pragma mark collection view cell paddings

– (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {

    return UIEdgeInsetsMake(10, 20, 10, 20); // top, left, bottom, right

}

– (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {

    return 0.5;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@”%ld”,(long)indexPath.row);

    

    

//    NibCell *cell = (NibCell*)[collectionView cellForItemAtIndexPath:indexPath];

//    NSArray *views = [cell.contentView subviews];

//    UIButton *label = [views objectAtIndex:0];

//    NSLog(@”Select %ld”,(long)label.tag);

}

/*

#pragma mark – Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

– (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/

– (IBAction)clsbtnactn:(id)sender

{

    [self.navigationController popToRootViewControllerAnimated:YES];

    

}

-(NSString *)convertHTML:(NSString *)html {

    

    NSScanner *myScanner;

    NSString *text = nil;

    myScanner = [NSScanner scannerWithString:html];

    

    while ([myScanner isAtEnd] == NO) {

        

        [myScanner scanUpToString:@”<“ intoString:NULL] ;

        

        [myScanner scanUpToString:@”>” intoString:&text] ;

        

        html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@”%@>”, text] withString:@””];

    }

    //

    html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

    

    return html;

}

@end

NibCell.h

#import <UIKit/UIKit.h>

//@protocol CellDelegate <NSObject>

//

//- (void)didClickOnCellAtIndex:(NSInteger)cellIndex withData:(id)data;

//

//@end

@interface NibCell : UICollectionViewCell

@property (strong, nonatomic) IBOutlet UIImageView *callimgvw;

@property (strong, nonatomic) IBOutlet UIImageView *imgvw;

@property (strong, nonatomic) IBOutlet UILabel *lbl1;

@property (strong, nonatomic) IBOutlet UILabel *lbl2;

@property (strong, nonatomic) IBOutlet UILabel *lbl3;

//@property (weak, nonatomic) id<CellDelegate>delegate;

//@property (assign, nonatomic) NSInteger cellIndex;

– (IBAction)pressButton:(id)sender;

@end

NibCell.m

#import “NibCell.h”

@implementation NibCell

@synthesize callimgvw,imgvw;

– (void)awakeFromNib {

    // Initialization code

}

– (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if (self) {

        // Initialization code

    }

    return self;

}

– (IBAction)pressButton:(id)sender {

    

    NSLog(@”%@”,sender);

    

        NSString *str = [NSString stringWithFormat:@”Button Clicked :: %ld”,(long)callimgvw.tag];

    

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@””

                                                        message:str

                                                       delegate:nil

                                              cancelButtonTitle:@”OK”

                                              otherButtonTitles:nil];

        [alert show];

    NSLog(@”%ld”,(long)callimgvw.tag);

}

@end

Download Sample Project From Github

Leave a comment