raju
raju

Reputation: 1

iphone multiple UITableviewController

I am new to iphone programming. How do i create multiple tableviewcontroller without creating too many class files.
Like in iphone safari bookmarkbar with every new folder created it, creates file and pushes. it will keep on creating tableviews.
How to achieve this.

Upvotes: 0

Views: 190

Answers (1)

Andy Obusek
Andy Obusek

Reputation: 12832

You can create/code a single class representing your generic UITableViewController, and then create multiple instances of it. For example, the original UITableViewController subclass loads to show the first page, then when a row is tapped, in your didSelectRowAtIndexPath method, instantiate another instance of your UITableViewController subclass and push it onto the navigation stack.

Remember your object oriented programming techniques here, a class is not an object, an object is an instance of a class, and there can be many instances of a class, which is what you need to achieve here.
Here's some sample code:

MyTableViewController.h

#import <UIKit/UIKit.h>
@interface MyTableViewController : UITableViewController
@end

MyTableViewController.m

#import "MyTableViewController.h"

@implementation MyTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Master", @"Master");
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
            self.clearsSelectionOnViewWillAppear = NO;
            self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
        }
    }
    return self;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //HERES THE IMPORTANT PART FOR YOU
    //SEE HOW I'M JUST CREATING ANOTHER INSTANCE OF MasterViewController?
    //You can tap the rows in this table until memory runs out, but all I have is one table view controller
    MyTableViewController *newController = [[[MasterViewController alloc] initWithNibName:@"MyTableViewController" bundle:nil] autorelease];
    [self.navigationController pushViewController:newController animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    }
    cell.textLabel.text = NSLocalizedString(@"Click Me", @"Click Me");
    return cell;
}
@end

Upvotes: 1

Related Questions