Reputation: 450
I have a categories table view controller. In my app, categories can have subcategories.
For instance, clothes can have 3, but maybe books can have no one and go directly to articles.
How can I do to only implement one categories table view controller, and reload with new values if category cell clicked has subcategories?
This is what I have but it wasn't successful:
- (void) loadCategories{
NSString *urlStr;
if (self.subCategoria){
urlStr = [NSString stringWithFormat:@"http://webservic.es/articulos.php?idc=%@&store=0", self.subCategoria];
}else{
urlStr = @"http://webservices.es/articulos.php";
}
NSLog(@"URL:%@", urlStr);
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:10.0];
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
categoria *categoria = [self.categoriasArray objectAtIndex:self.tableView.indexPathForSelectedRow.row];
if (categoria.subcategoria){
self.subCategoria = categoria.subcategoria;
[self viewDidAppear:YES];
[self.tableView reloadData];
}else{
ArticulosController *articulosController =segue.destinationViewController;
articulosController.id_categoria = categoria.id_categoria;
}
}
Thanks
Upvotes: 0
Views: 71
Reputation: 126167
There are two ways to consider your question. Do you want to have a single TableViewController class (and possibly multiple instances of it), or a singleton instance of that class?
I'd say the latter is more trouble than it's worth. The kit isn't designed to work this way, so all of the shortcuts it provides you become work you'll have to do yourself -- and the best you get from this is a modest improvement in memory usage. (More likely you'll have bugs introduced because it's easy to miss something when reconfiguring the same instance to represent different data.)
On the other hand, reusing a single view controller class is something the kit is designed to help you with, and since it sounds like you're using storyboards already, you're halfway there. In your prepareForSegue:sender:
implementation, you'll always have a destinationViewController -- so all you need to do is configure it to represent the subcategory (by setting its subcategory
property, it looks like).
(In fact, if you're using storyboard segues, you're getting new instances of your class created for you whether you like it or not... if you really want to go for the singleton approach, you'll have to abandon the segue, which means you'll lose the built-in navigation stack behavior.)
Upvotes: 1