Pregunta

I have used a Table View with Dynamic Prototype cells.

I have created a sample row in the table view with four values of a quantity:

Title
Min Value
Max Value
Current value.

I have created Table view controller and table view cell classes for accessing those properties in my code.

Now I have to set the values dynamically. I can see people suggesting to pass array of values separately for each.

Like:

Array 1 for Title : which will display title for all items in the row
Array 2 for Min value and so on.....

Is there a way that we pass an array of objects to the table view and it creates the table.

Please suggest.

¿Fue útil?

Solución

I can see people suggesting to pass array of values separately for each.

Don't do this it is very poor approach, instead create a single array of NSDictionnaries, for instance:

_listOfValue =
@[
@{@"Title":@"title 1", @"Min Value":@"0", @"Max Value":@"100", @"Current value":@"50"},
@{@"Title":@"title 2", @"Min Value":@"4", @"Max Value":@"90", @"Current value":@"60"},
@{@"Title":@"title 3", @"Min Value":@"6", @"Max Value":@"70", @"Current value":@"70"}
];

This will make it easy to retrieve the data you need since they are not separated.

In numberOfRowsInSection you can return [self.listOfValue count].

In cellForRowAtIndexPath or didSelectRowAtIndexPath you can easily get the dictionnary at the IndexPath then parse the value of each key.

//Get the specific value
NSDictionnary *valueDict = _listOfValue[indexPath.row];

//read data from value dictionary
valueDict[@"Title"];
//or the old syntax 
[valueDict objectForKey:@"Title"];

Otros consejos

No, you can't pass the array direct to the table. Your table view controller would usually maintain the array (often a single array containing dictionaries or custom class instances) and your code in the table view controller uses that array to configure the cells.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top