Pergunta

Let me start by saying that I can pass the data across a prepareForSegue method however I believe the way I am doing it is not optimal and I am always looking to improve my coding skills. I am seeing if this is the correct way to do it? It works but might be 'fundamentally' wrong.

I have a plist populating a uitableview

// Find out the path of recipes.plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"law" ofType:@"plist"];

// Load the file content and read the data into arrays
self.dataArray = [NSArray arrayWithContentsOfFile:path];

//Sort the array by section
self.sortedArray = [self.dataArray sortedArrayUsingDescriptors:@[
                [NSSortDescriptor sortDescriptorWithKey:@"Section" ascending:YES],
                [NSSortDescriptor sortDescriptorWithKey:@"Title" ascending:YES]]];

//Title
self.namesArray = [self.sortedArray valueForKeyPath:@"Title"];

self.sectionArray = [self.dataArray valueForKeyPath:@"Section"];

//Legislation
self.legislationArray = [self.sortedArray valueForKeyPath:@"Legislation"];

I am passing the values through the prepare for segue method as so

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
 if ([segue.identifier isEqualToString:@"detailAll"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    DetailTableViewController *destViewController = segue.destinationViewController;
    destViewController.title = [self.namesArray objectAtIndex:indexPath.row];
    destViewController.nameLabel = [self.namesArray objectAtIndex:indexPath.row];
    destViewController.legislationLabel = [self.legislationArray objectAtIndex:indexPath.row];
 }
}

This works. However, is there a cleaner way to do this? The values are being passed to another array and then populated into a detail view UItableview

Foi útil?

Solução

You're not using specific model objects, so your current code is fine.

However, a more robust way of working would be to define an object which represents the model object you're representing in each cell. This would have properties representing the name, section and legislation. Your tableview would then configure each cell based on the object, and the datasource hold a single array instead of multiple ones for each property.

When you pushed to a detail view controller, you'd just pass this model object across.

This approach has several advantages:

  • Less code
  • More extensible (if you added another property to your model object, you wouldn't need to add yet another array, and property on your detail view controller)
  • Clearer to understand

Making model objects is very simple, particularly now there's autocompletion of properties. Just make a NSObject subclass, and declare your properties in the header:

@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *name;
@property(nonatomic,copy) NSString *legislation;

That's all there is to it. The compiler will auto-synthesise these properties for you.

Outras dicas

I've created a library for this, check it out! https://github.com/stefanomondino/SMQuickSegue

Basically you pass over (during PERFORM segue, forgetting about prepareForSegue) a dictionary of property/values that will be set on destination view controller if it is responding to corresponding selectors (it will skip a wrong parameter otherwise).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top