i working json parsing using nsserialization. have json data , want parse in uicollectionview. json api data.
{ "result": "successful", "data": [ { "id": "2", "product_name": "6\" round plate", "sku": "ecow6rp", "description": "ideal serving finger foods , snacks. safe , healthy serving option. made 100% sugarcane pulp, these plates can used in microwaves , freezers. \r\n\r\ndiameter 6.0 (inch) x depth 0.6 (inch)", "price": "100.00", "business_price": "90.00", "image": "14531965421452838128ecow6rp_02.jpg", "pack_size": "10", "business_pack_size": "50", "category": "2,3", "tax_class": "1", "created": "2016-01-19", "altered": "2016-01-21 11:13:42", "status": "1", "deleted": "0" }, { "id": "3", "product_name": "bio bags (large)", "sku": "ecowgbl", "description": "bio bags (large) description ", "price": "100.00", "business_price": "90.00", "image": "14531967251452837823ecowgbs_03.jpg", "pack_size": "10", "business_pack_size": "50", "category": "2,3", "tax_class": "1", "created": "2016-01-19", "altered": "2016-01-21 11:14:37", "status": "1", "deleted": "0" }, { "id": "4", "product_name": "bio bags (medium)", "sku": "ecowgbm", "description": "bio bags (medium) description \r\n", "price": "100.00", "business_price": "90.00", "image": "14531968711452837959ecowgbm_03.jpg", "pack_size": "10", "business_pack_size": "50", "category": "2,3", "tax_class": "1", "created": "2016-01-19", "altered": "2016-01-21 11:14:47", "status": "1", "deleted": "0" }, { "id": "5", "product_name": "bio bags (small)", "sku": "ecowgbs", "description": "bio bags (small) description", "price": "74.00", "business_price": "64.00", "image": "14531969571452838088ecowgbl_02.jpg", "pack_size": "10", "business_pack_size": "50", "category": "2,3", "tax_class": "1", "created": "2016-01-19", "altered": "2016-01-21 11:14:59", "status": "1", "deleted": "0" }, { "id": "6", "product_name": "7\" round plate", "sku": "ecow7rp", "description": "ideal serving finger foods , snacks. safe , healthy serving option. made 100% sugarcane pulp, these plates can used in microwaves , freezers. \r\n\r\ndiameter 7.2 (inch) x depth 0.6 (inch)", "price": "100.00", "business_price": "90.00", "image": "14531971831452838830ecow7rp_01.jpg", "pack_size": "10", "business_pack_size": "50", "category": "2,3", "tax_class": "1", "created": "2016-01-19", "altered": "2016-01-21 11:14:17", "status": "1", "deleted": "0" } ] }
this json data want parse product_name, id, image
in uicollectionview cell. code below.
- (void)viewdidload { [super viewdidload]; [uiapplication sharedapplication].networkactivityindicatorvisible = yes; nsurl *url = [nsurl urlwithstring:@"http://dev1.brainpulse.org/ecoware1/webservices/products/2"]; nsurlrequest *request= [nsurlrequest requestwithurl:url]; [nsurlconnection connectionwithrequest:request delegate:self]; _collectionview.delegate = self; _collectionview.datasource = self; } -(void)connection:(nsurlconnection *)connection didreceiveresponse:(nonnull nsurlresponse *)response{ data = [[nsmutabledata alloc] init]; nslog(@"did receive response"); } -(void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)thedata{ [data appenddata:thedata]; nslog(@"daata"); } -(void)connectiondidfinishloading:(nsurlconnection *)connection { [uiapplication sharedapplication].networkactivityindicatorvisible = no; productname = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil]; [_collectionview reloaddata]; } -(void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { uialertview *errorview= [[uialertview alloc]initwithtitle:@"error" message:@"the download not complete please make sure you're connected internet" delegate:nil cancelbuttontitle:@"dismiss" otherbuttontitles: nil]; [errorview show]; [uiapplication sharedapplication].networkactivityindicatorvisible = no; } - (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return [productname count]; } -(uicollectionviewcell*)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath { onetimecell *cell = (onetimecell*)[collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath]; // cell.productname.text = [[productname objectatindex:indexpath.row] objectforkey:@"product_name"]; nsarray *myarr = [productname objectatindex:indexpath.row]; cell.productname.text = [[myarr objectatindex:0] objectforkey:@"result"]; return cell; }
i want update image directly url, make url adding object key image , wan parse image on image view please see code below
nsstring *image = [[productname objectatindex:indexpath.row] objectforkey:@"image" ]; nslog(@"image %@", image); nsurl *baseurl = [nsurl urlwithstring:@"http://dev1.brainpulse.org/ecoware1//app/webroot/img/uploads/product/thumb/"]; nsurl *url = [nsurl urlwithstring:image relativetourl:baseurl]; nsurl *absurl = [url absoluteurl]; nslog(@"absurl = %@", absurl); cell.productimage.image = [uiimage imagewithdata:[nsdata datawithcontentsofurl:[nsurl urlwithstring:absurl]]];
step-1
allocate memory of nsmutablearray
in viewdidload
,
- (void)viewdidload { [super viewdidload]; productname = [nsmutablearray new]; }
step-2
-(void)connectiondidfinishloading:(nsurlconnection *)connection { [uiapplication sharedapplication].networkactivityindicatorvisible = no; nsarray *arr = [[nsjsonserialization jsonobjectwithdata:data options:0 error:nil]objectforkey:@"data"]; [productname removeallobjects]; (nsdictionary *tmp in arr) { nsmutabledictionary *temp = [nsmutabledictionary new]; [temp setobject:[tmp objectforkey:@"product_name"] forkey:@"product_name"]; [temp setobject:[tmp objectforkey:@"id"] forkey:@"id"]; [temp setobject:[tmp objectforkey:@"image"] forkey:@"image"]; [productname addobject:temp]; } if (productname) { [_collectionview reloaddata]; } }
step-3
-(uicollectionviewcell*)collectionview:(uicollectionview *)collectionview cellforitematindexpath:(nsindexpath *)indexpath{ onetimecell *cell = (onetimecell*)[collectionview dequeuereusablecellwithreuseidentifier:@"cell" forindexpath:indexpath]; cell.productname.text = [[productname objectatindex:indexpath.row] objectforkey:@"product_name"]; return cell; }
Comments
Post a Comment