views:

594

answers:

3

I am working in a app where i have data in UITableView. It is like a drill down application. User will click on a row and will go to next page showing more records in a UITableView. But problem in my case is that i dont know upto how many level user can drill. The number of levels are not fixed. So now i am thinking to create and add the viewcontrollers programmatically. Is it possible?? if yes how? thanks in advance.

A: 

Here's a starting point:

http://chris-software.com/index.php/2009/04/29/creating-a-view-programmatically/

Joost Schuur
it s about creating uiView programmically but i need to create UIViewController programmically..........
pankaj
A: 
UIViewController *controller = [[UIViewController alloc] init];
controller.view = whateverViewYouHave;

Do you have your own view controller that you coded? In that case you probably don't need to set the view property as it has been set in IB if that is what you used. When you have your controller you can push it onto the navigationController or view it modally etc.

willcodejavaforfood
Right now i am on a viewcontroller and want to direct the user to new viewcontroller and display data in a tableview
pankaj
and yes, one more thing user can also further drill down to next level from this new programmically created viewcontroller
pankaj
Then you probably want your first view controller to be a UINavigationController.
willcodejavaforfood
A: 

UIViewControllers are always created programmatically. It sounds like you just need to have the same class for each level of view controller, e.g.:

//CoolViewController:UITableViewController
//CoolViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (!self.isAtTopLevel) {
        CoolViewController *cvc = [[CoolViewController alloc] initWithRecord:[self.records objectAtIndex:indexPath.row]];
        [self.navigationController pushViewController:cvc animated:YES];
        [cvc release];
    } else {
        //do something else
    }
}

In this case, thingies would be some sort of recursive NSArray (i.e. an array of arrays).

eman
Thanks eman for answer, in this new view controller i need to display a UITableview with records. How will i do that?
pankaj
and yes, one more thing user can also further drill down to next level from this new programmically created viewcontroller
pankaj
(updated example) In this example, each table view controller would have an array of records, each of which would have an array of sub-records (correct me if I'm not understanding the design correctly). So you could have a class called `Record`, which would have a name (displayed on the table cell) and an `NSArray` of `Record`s--each table view controller would display the array of sub-records.
eman
according to ur last comment i will have have to bring all rows from my database and save it. It is not my possible as that way i will be ended up saving hundreds of thousands of rows and only using only few of them
pankaj
@pankaj: it's hard to know your desired design based on your description. Can you post more specifics? (Are you using Core Data?)
eman
ok, i m having a tableview where i am showing some data fetched from web services. There could be thousands of rows in the database but i want to display them in a drill down way as there is some relation in them. Now on first page i display main categories. When user selects any one of them it will display the further sub categories. I dont know upto how many levels i will be getting the records, so i cant have a fixed number of viewcontrollers. So i trying to generate them programmically. When user selects a row i want to create a view controller, display it with tableview and records....
pankaj
.... and when user selects any of the row in that table view(created dynamically) it should again be redirected to new view controller(dynamically created). I hope i am more clear now
pankaj