简体   繁体   中英

How to make lots of view controllers fast, in interface builder?

I'm making an app which has a tableview and a DetailViewController. I'm using Apple's "MultipleDetailViews" code as a launchpad. I want to make this app have around 100 rows in the split view and I want the detail view to change for each row (read 100 views). How can I do this using interface builder but without generating 100 class files and changing names repeatedly?

Currently the only method I have is to create separate view controllers (with class files) manually. This is very agitating, however.

Is there anyway I can use one DetailViewController, add several views to it inside interface builder and push each view when I choose a row in the tableview.

On each view I want to add a background image and three buttons that contain different sounds (every row's view will have three unique sounds). How can I only create three IBActions and change the sound file path according to what row is selected?

Is there any time-efficient way to do what I am asking?

100 view controllers classes? That's no good.

100 instances of a single view controller class? I should hope not.

Let's get you the behavior you described with 2, just 2. You have a controller for your table view and a controller for your detail view. That's it.

When you select a row in the table view pass that row index to the detail view controller and give the detail view controller a way to load the correct image and sounds based on that row.

That could be from a naming convention for your image and sound resources ('background0', 'background1', ...) or it could be from some config file which defines the the background image and sounds for each row (a plist containing an array of dictionaries: [{background:"moon.png", firstSound:"clown.mp3", secondSound:"moose.mp3", thirdSound:"water.mp3}, {...}, ...]).

It sounds like each of your detail views will be similar enough that you can create a single UIViewController instance and reconfigure it each time a cell is tapped. Here is an example of how you might alter the MultipleDetailViews project to change the background color of a single UIViewController instance depending on which row is selected.

static const NSUInteger kRowCount = 100;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.contentSizeForViewInPopover = CGSizeMake(310.0, self.tableView.rowHeight*kRowCount);
    // Create an array of colors to cycle through
    self.colors = [NSArray arrayWithObjects:[UIColor redColor], [UIColor greenColor], [UIColor blueColor], nil];
}

#pramga mark - UITableViewDataSource methods

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

- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Dequeue or create cell
    cell.textLabel.text = [NSString stringWithFormat:@"View Controller #%d", indexPath.row + 1];
    return cell;
}

#pramga mark - UITableViewDataDelegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Don't create a new view controller, just reconfigure the on that is already displayed
    DetailViewController *dvc = [self.splitViewController.viewControllers objectAtIndex:1];
    dvc.view.backgroundColor = [colors objectAtIndex:(indexPath.row % [colors count])];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM