Verma
Verma

Reputation: 267

How to take particular value from array and show in tableview

I Have array and save all values in an arraypartno and i put that particular value in label and want to show in my cell like PARTNUMBER:-

Here's the code:-

 for (int i =0 ; i<[arrData count]; i++)


   {    
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"Condition"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"DateCode"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"MFG"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"Qty"]];
        [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"id"]];
    }

So what i have to write in lblplate1.text for particular key,now it show the whole array values.

UILabel *lblplate1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 200, 30)];

lblplate1.text = [arraypartno  objectAtIndex:indexPath.row];
lblplate1.textColor = [UIColor colorWithRed:2.0/255.0 green:143.0/255.0 blue:213.0/255.0 alpha:1];

lblplate1.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:lblplate1];

Upvotes: 0

Views: 167

Answers (2)

Anil Kothari
Anil Kothari

Reputation: 7733

If you are working for a client Application then try to adopt MVC. by storing the data in the form of model objects with the properties like condition,DateCode,MFG,Partno etc and store data in Array of Model Objects.

for (int icounter=0; icounter<[arrData count]; icounter++) {
                    if (objModalClass!=nil) {
                        [objModalClass release];
                        objModalClass=nil;
                    }
                    objModalClass=[[ModalClass alloc]init];
                    objModalClass.Condition=[[arrData objectAtIndex:i] valueForKey:@"Condition"];
                    objModalClass.DateCode=[[arrData objectAtIndex:i] valueForKey:@"DateCode"];;
                    objModalClass.partno=[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"];
                    [arrList addObject:objModalClass];
                }
}

Retrive Data From Model Class Objects

for (ModalClass *obj in arrList){
           lblCondition.text=obj.Condition;
}

Try to use MVC as it is easy for you to change the structure in case you have to add new properties or delete them.

Upvotes: 1

Aman Aggarwal
Aman Aggarwal

Reputation: 3754

Instead of adding different Key Values to same array, make different arrays for different keys

Like this

    [arrayCondition addObject:[[arrData objectAtIndex:i] valueForKey:@"Condition"]];
    [arrayDateCode addObject:[[arrData objectAtIndex:i] valueForKey:@"DateCode"]];
    [arrayMFG addObject:[[arrData objectAtIndex:i] valueForKey:@"MFG"]];
    [arraypartno addObject:[[arrData objectAtIndex:i] valueForKey:@"PARTNUMBER"]];
    [arrayQty addObject:[[arrData objectAtIndex:i] valueForKey:@"Qty"]];
    [arrayID addObject:[[arrData objectAtIndex:i] valueForKey:@"id"]];

And in cellforRowAtIndexPath

lblplate1.text = [arraypartno  objectAtIndex:indexPath.row];

Note:- Please initialize array in viewDidLoad.

Upvotes: 0

Related Questions