简体   繁体   中英

How to assign a specific ViewController to a UIView (in UICollectionView)?

I searched the web for hours but I can't find a solution to my problem.

I have a View in my storyboard that contains a UICollectionView . I've set the cells of the UICollectionView to be really big (only one can appear on the screen, you have to swipe horizontally to see the others). In my application, I use this layout to ask the user some questions, one after another. The user can respond to those questions by touching buttons, move around a map, select a date, etc... Each questions have the same layout; they are displayed in a box, with a title label and a button. That's why I chose to use a UICollection View.

I want to display a specific view (the options that allows the user to answer (buttons, Map, DatePicker, etc..)) for each questions in the blue rectangle (see picture below).

A screenshot of my view

My idea was to create multiple ViewControllers in my StoryBoard and be able to tell : "ok, for the first question (first cell), put the view XXX in the container, for the second, put the view YYY, etc...".

How can I do this ?

I've tried to add a ContainerView but we can only link one view in the storyboard.

I want to able to do something like this :

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! SearchQuestionBox
    cell.questionAnswersZone.view = storyboard.viewNumberOne  //Just for the idea haha

    return cell

}

I would suggest not using multiple view controllers.

Instead you could just add the views for each cell to the one main view controller. Keep in mind you can set a views frame to be offscreen.

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