MendyK
MendyK

Reputation: 1488

How should I use Viewcontrollers

I'm making an app with 2 table views. the first has a bunch of cells which lead to the next table view (which can have different data depending on which cell is selected). My question is, is it better to have a bunch of view controllers for the second menu (1 for each cell selection, or to have one view controller and load different data on it.

Upvotes: 0

Views: 74

Answers (3)

user1564913
user1564913

Reputation:

There is no right or false. However, I would recommend you to use different viewcontrollers. Especially if you are using a storyboard, this is very straightforward. Just connect your different cell types with the appropriate viewcontroller and pass your data in the -performSegueWithIdentifier: method. Maybe if you would add some details about the kind of data etc. I could give you a more adequate answer.

Edit:

In this case it would actually make more sense to work with a single second tableviewcontroller, as the input format is always the same and the output is based on the input data. You could do something like this:

FirstTableViewController.h

@interface FirstTableViewController.h : UITableViewController

// array containing NSArrays which themselves contain NSStrings
@property (nonatomic, strong) NSArray *textArrays;

@end

FirstTableViewController.m

@implementation FirstTableViewController

@synthesize textArrays;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return self.textArrays.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Your Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier forIndexPath:indexPath];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    NSArray *textArray = self.textArrays[indexPath.row];
    if (textArray != nil && [textArray isKindOfClass:[NSArray class]]) {
        // configure cell
    }

    return cell;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell *cell = (UITableViewCell *)sender;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    SecondTableViewController *controller = [segue destinationViewController].
    controller.textArray = self.textArrays[indexPath.row];
}

@end

SecondTableViewController.h

@interface SecondTableViewController.h : UITableViewController

// array containing NSStrings
@property (nonatomic, strong) NSArray *textArray;

@end

SecondTableViewController.m

@implementation SecondTableViewController

@synthesize textArray;

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return self.textArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Your Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier forIndexPath:indexPath];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }

    NSString *text = self.textArray[indexPath.row];
    if (text != nil && [text isKindOfClass:[NSString class]]) {
        // configure cell, e.g. cell.textLabel.text = text;
    }

    return cell;
}

@end

Remember that there are always several ways to achieve something. This is just my way and it does not have to be yours. However, I hope this helps.

Upvotes: 3

pnavk
pnavk

Reputation: 4632

I would use 2 TableViewController and Custom Cells to do this. Basically once the user chooses a cell on the first TableView, you populate the second Table View with the appropriate data and dequeue the appropriate custom cell to display the data.

Upvotes: 0

Caleb
Caleb

Reputation: 124997

Make sure you understand the difference between a class and an instance of that class, i.e. an object. Typically, when a user taps on a cell in your first table, you'll create a new view controller object, load it up with whatever data it needs to display the content corresponding to the tapped cell, and push it onto your navigation stack. When the user is done looking at that content, they hit the "back" button to switch back to the previous view controller. When that happens, the "detail" view controller is normally deallocated because no other objects are referencing it. When the user taps another cell, the process repeats -- a new view controller is created, configured, pushed, and eventually dismissed.

So, that means you'll have two view controller classes, and at any given time one or two view controller objects (not counting a navigation controller), but over time you end up creating many instances of the detail view controller class. You could create just a single instance of the detail view controller and reuse it as necessary, but there's no real advantage to doing so and it ends up being another thing that the main view controller needs to keep track of.

Of course, if different cells in your main table could lead to significantly different kinds of data that need to be displayed in different ways, then you might end up with more than just the two view controller classes. In that case, when the user taps a cell you'd have to figure out which kind of detail view controller you want to use and then instantiate that one. For example, tapping one cell might lead to information about a person while another might lead to a picture and a third might lead to a map.

Upvotes: 0

Related Questions