vshnu vshnu - 4 months ago 9
iOS Question

How to display only the name based on id

I have to display only the stone_name. At first I have stored the data in one array. In array I am geting the data but when I try to display in tableViewCell it is returning as nil. Here is my code please help me where I am doing wrong

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

if (self.Viewforbrandfilter.hidden==YES) {
return 0;

}else if (tableView==tblstone)
{


if ([self.arrstone count]>0) {

return [self.arrstone count];
}

}

return [arrbradfilter count];

}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView==tblstone) {
return 1;
}

return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellidentifier =@"cell";
Filtercell *cell = (Filtercell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

if (cell == nil)
{
cell = [[Filtercell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}

cell.lblfltrs.text=[arrbradfilter objectAtIndex:indexPath.row];

if (indexPath.row==3 || indexPath.row==4 || indexPath.row==5 || indexPath.row==6 || indexPath.row==7 || indexPath.row==8) {
cell.swtchfltrs.hidden=true;
}

if (tableView==tblstone) {



NSString *cellidentifier =@"mycell";
StoneCell *cell = (StoneCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

cell = [[StoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];



for (NSDictionary *item in self.arrstone) {
NSDictionary *stn = [item valueForKey:@"productStone"];
if (!stn==nil) {
NSDictionary *stone = [stn valueForKey:@"stone"];
if (!stone==nil) {
NSString *stnname = [stone valueForKey:@"stone_name"];

NSLog(@"aaaaa %@:",stnname);

// cell.lblstones.text=[self.arrstone objectAtIndex:indexPath.row];


{
return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView==tblmenu) {

if (indexPath.row==3) {


[self callstoneservice];
self.tblstone.hidden=NO;
[UIView animateWithDuration:0.15 animations:^{

self.tblstone.alpha=1.0;

}];

}
}
}


Here i have called API

-(void)callstoneservice
{
[self showHUD];
WebserviceOperation *objWebservice=[[WebserviceOperation alloc]initWithDelegate:self callback:@selector(stoneResponse:)];

[objWebservice getstone:@"1"];
}


-(void)stoneResponse:(id)sender
{
[self killHUD];
if([sender isKindOfClass:[NSError class]])
{

}
else
{
self.arrstone=(NSMutableArray *)sender;
if(self.arrstone.count>0)
{
[self.tblstone reloadData];
}
}
}


this is my response

[
{
"productVariations": [
{
"product": 1,
"product_number": "2016/07/06",
"is_stone": 1,
"is_default": 1,
"is_beeds": 1,
"is_ennamal": 1,
"gross_weight": 222,
"net_weight": 20,
"stone_weight": 21,
"pure_weight": null,
"price_product": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"productMetal": [],
"productStone": [
{
"productVariations": 1,
"stone_code": null,
"nature": {
"nature": "Semi Precious",
"id": 2,
"createdAt": null,
"updatedAt": null
},
"stone": {
"stone_name": "Ruby",
"id": 2,
"createdAt": null,
"updatedAt": null
},
"color": {
"color_name": "Red",
"id": 1,
"createdAt": null,
"updatedAt": null
},
"clarity": {
"clarity_name": "No Clarity",
"id": 1,
"createdAt": null,
"updatedAt": null
},
"cuttype": {
"cuttype_name": "No Cut Type",
"id": 1,
"createdAt": null,
"updatedAt": null
},
"shape": 1,
"size": 1,
"nos": 1,
"weight_carat": 1,
"weight_gms": 0,
"price": null,
"id": 3,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-06-13T07:56:13.000Z"
}
]
},
{
"product": 1,
"product_number": "2016/07/01",
"is_stone": 2,
"is_default": null,
"is_beeds": null,
"is_ennamal": null,
"gross_weight": 111,
"net_weight": 10,
"stone_weight": 11,
"pure_weight": null,
"price_product": null,
"id": 10,
"createdAt": "2016-07-02T02:17:04.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"productMetal": [],
"productStone": []
}
],
"productCollection": [
{
"product": {
"design_name": "Cozy Coracle A",
"description": null,
"is_stone": 1,
"is_collection": 1,
"is_variation": null,
"is_releated": 1,
"number_of_variation": 1,
"created_by": null,
"modify_by": null,
"active_status": null,
"delete_status": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-06-13T07:56:13.000Z",
"company": 1,
"category": 1,
"brand": 1
},
"variation": {
"product_number": "2016/07/06",
"is_stone": 1,
"is_default": 1,
"is_beeds": 1,
"is_ennamal": 1,
"gross_weight": 222,
"net_weight": 20,
"stone_weight": 21,
"pure_weight": null,
"price_product": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"product": 1
},
"collectionVariation": {
"product_number": "2016/07/06",
"is_stone": 1,
"is_default": 1,
"is_beeds": 1,
"is_ennamal": 1,
"gross_weight": 222,
"net_weight": 20,
"stone_weight": 21,
"pure_weight": null,
"price_product": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"product": 1
},
"id": 1,
"createdAt": "2016-06-13T18:56:45.000Z",
"updatedAt": "2016-06-13T18:56:45.000Z"
},
{
"product": {
"design_name": "Cozy Coracle A",
"description": null,
"is_stone": 1,
"is_collection": 1,
"is_variation": null,
"is_releated": 1,
"number_of_variation": 1,
"created_by": null,
"modify_by": null,
"active_status": null,
"delete_status": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-06-13T07:56:13.000Z",
"company": 1,
"category": 1,
"brand": 1
},
"variation": {
"product_number": "2016/07/01",
"is_stone": 2,
"is_default": null,
"is_beeds": null,
"is_ennamal": null,
"gross_weight": 111,
"net_weight": 10,
"stone_weight": 11,
"pure_weight": null,
"price_product": null,
"id": 10,
"createdAt": "2016-07-02T02:17:04.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"product": 1
},
"collectionVariation": null,
"id": 19,
"createdAt": "2016-07-12T14:18:41.000Z",
"updatedAt": "2016-07-12T14:18:41.000Z"
}
],
"productRelation": [
{
"product": {
"design_name": "Cozy Coracle A",
"description": null,
"is_stone": 1,
"is_collection": 1,
"is_variation": null,
"is_releated": 1,
"number_of_variation": 1,
"created_by": null,
"modify_by": null,
"active_status": null,
"delete_status": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-06-13T07:56:13.000Z",
"company": 1,
"category": 1,
"brand": 1
},
"variation": {
"product_number": "2016/07/06",
"is_stone": 1,
"is_default": 1,
"is_beeds": 1,
"is_ennamal": 1,
"gross_weight": 222,
"net_weight": 20,
"stone_weight": 21,
"pure_weight": null,
"price_product": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"product": 1
},
"relatedVariation": 2,
"id": 1,
"createdAt": "2016-06-13T18:56:23.000Z",
"updatedAt": "2016-06-13T18:56:23.000Z"
},
{
"product": {
"design_name": "Cozy Coracle A",
"description": null,
"is_stone": 1,
"is_collection": 1,
"is_variation": null,
"is_releated": 1,
"number_of_variation": 1,
"created_by": null,
"modify_by": null,
"active_status": null,
"delete_status": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-06-13T07:56:13.000Z",
"company": 1,
"category": 1,
"brand": 1
},
"variation": {
"product_number": "2016/07/06",
"is_stone": 1,
"is_default": 1,
"is_beeds": 1,
"is_ennamal": 1,
"gross_weight": 222,
"net_weight": 20,
"stone_weight": 21,
"pure_weight": null,
"price_product": null,
"id": 1,
"createdAt": "2016-06-13T07:56:13.000Z",
"updatedAt": "2016-07-25T13:59:19.000Z",
"product": 1
},
"relatedVariation": {
"product_number": "2016/07/09",
"is_stone": 1,
"is_default": null,
"is_beeds": 2,
"is_ennamal": 2,
"gross_weight": null,
"net_weight": null,
"stone_weight": null,
"pure_weight": null,
"price_product": null,
"id": 3,
"createdAt": "2016-06-27T13:25:37.000Z",
"updatedAt": "2016-07-25T08:34:36.000Z",
"product": null
},
"id": 2,
"createdAt": "2016-06-14T05:37:58.000Z",
"updatedAt": "2016-06-14T05:37:58.000Z"
}
],

Answer

Incorrect Mapping of Array to Dictionary.. Check the changed code... this should work.

   -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    NSString *cellidentifier =@"cell";
    Filtercell *cell = (Filtercell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

    if (cell == nil)
    {
        cell = [[Filtercell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
    }

    cell.lblfltrs.text=[arrbradfilter objectAtIndex:indexPath.row];

    if  (indexPath.row==3) {
        cell.swtchfltrs.hidden=true;
    }if (indexPath.row==4) {
        cell.swtchfltrs.hidden=true;
    }if (indexPath.row==5) {
        cell.swtchfltrs.hidden=true;
    }if (indexPath.row==6) {
        cell.swtchfltrs.hidden=true;
    }if (indexPath.row==7) {
        cell.swtchfltrs.hidden=true;
    }if (indexPath.row==8) {
        cell.swtchfltrs.hidden=true;
    }

    if (tableView==tblstone) {



        NSString *cellidentifier =@"mycell";
        StoneCell *cell = (StoneCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];

        cell = [[StoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];



        for (NSDictionary *item in self.arrstone) {

//Changes Starts
            NSArray *productStoneItem = [item valueForKey:@"productStone"]
            NSDictionary *stn = [productStoneItem objectAtIndex:0];
//Changes Ends
            if (!stn==nil) {
                NSDictionary *stone = [stn valueForKey:@"stone"];
                if (!stone==nil) {
                    NSString *stnname = [stone valueForKey:@"stone_name"];

                    NSLog(@"aaaaa %@:",stnname);

                   // cell.lblstones.text=[self.arrstone objectAtIndex:indexPath.row];


    {
              return cell;

    }