1

I have an Array of 5 items. This array is dynamic therefore the number of items may vary.

I have aUIVIewController as shown in the following image. In my UIView, there are few components (Like buttons, etc). Based on the number of items in the above array i want to add the UIVIew to my UIViewContorlleras shown in the image.

For example: There're 5 items in the Array, then i need to add 5 UIView's to my UIViewController.

enter image description here

1.) I don't want to use a XIBfile for the UIView but want to use only StoryBoard. How can i design the UIView in StoryBoard ?

2.) How can i add UIView to the UIViewController dynamically as the number of items in the array increased ?

2
  • 1
    explain more thing about view you want to add. like it's height width and you want to add this view sideByside or oneByone verticle?? Commented Apr 9, 2015 at 11:12
  • This is a bog standard collection view. Don't try to recreate this as Apple have already done the hard work keeping track of layout and wrapping the views as required. There is an answer below that details how to go about this, I suggest you follow that and save yourself a lot of grief. Commented Apr 9, 2015 at 18:20

4 Answers 4

1

Loop through your array (I have assumed your array contains UIViews, if not you may update accordingly) like:

for(UIView *subView in arrayOfItems){
       subView.position = specify the position 
       [self.view addSubview:subView];
}
Sign up to request clarification or add additional context in comments.

Comments

0

1) You need to create a class, where the properties have IBOutlets, like this:

@interface MyView ()

@property (weak, nonatomic) IBOutlet UIView *viewContent;

@end

Than you can design your UIView inside the UIStoryboard and connect them from the class to the ui element. In previous xCode versions there was always a problem if you want to drag from UIStoryboard to the class, have to do it the different way.

2) To add a UIView to your UIViewController you just have to add it as subView inside your UIViewController like this:

[self.view addSubView:anyView]

Comments

0

set the frame of the sub views as per your design

 for (int i=0;i<YourViewArrayCount; i++) {
            [self.view addSubview:[YourViewArrayCount objectAtIndex:i]];

        }

1 Comment

How can i append it to the next line. For example if only 2 UIVIews fits a row, how will i append it to the next line ?
0

create a UICollectionView in the storyboard , and add a UICollectionViewCell prototype with a UIButton in it , and in the UICollectionViewDataSource method numberOfItemsInSection return your array count:

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return yourArray.count;
}

and in order to handle the button touch event ,you'll have to set the UIButton tag to 999 for example in the UICollectionViewCell prototype in storyboard,and in the UICollectionViewDataSource method cellForItemAtIndexPath get a reference to the button using its tag then add the touch event handler to it programmatically :

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCellIdentifier" forIndexPath:indexPath];

    UIButton *button = [cell viewWithTag:999];

    [button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

finally to find out which button was actually touched (at which indexPath) , i suggest subclassing the UIButton class and add a property of type NSIndexPath (don't forget to change the button class in storyboard to your new UIButton subclass) and in the UICollectionViewDataSource method cellForItemAtIndexPath do the following

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"yourCellIdentifier" forIndexPath:indexPath];

    MySepcialButton *button = [cell viewWithTag:999];

    button.indexPath = indexPath;

    [button addTarget:self action:@selector(buttonTouchHandler:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

this way in the buttonTouchHandler: method you can check the button property indexPath.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.