title: Adding subviews dynamically into the main view on button click
body: <p>I wanted to create a main view which consists of a add button. </p>
  
  <p>On each click on the add button a subview must be added in the main view.the subview is present in another class which consists of a close button,on click of the close button  the subview class informs the main view through delegate function and the particular subview mist get removed from the main view. </p>
  
  <p>The remaining subviews must be repositioned in the main view.</p>
  
  <p>I want the subviews to be added in the main view in <strong>matrix form(2xN)</strong> matrix. That is 2 subviews in one rows and n number of columns. </p>
  
  <p>I have implemented two methods for the same, but the subviews are not getting repositioned.</p>
  
  <p><strong>method 1 :</strong></p>
  
  <pre><code>    - (IBAction)btnAddView:(id)sender
      {
  
          if ((count%2)==0)
          {
              NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(10, 10+       (i*120), 100, 100) andDelegate:self];
             // [self.view addSubview:subView];
              [arr addObject:subView];
              i++;
  
          }
          else if ((count%2)==1)
          {
              NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(120, 10+(j*120), 100, 100) andDelegate:self];
             // [self.view addSubview:subView];
              [arr addObject:subView];
              j++;
          }
  
           count = count+1; 
  
          for (int x=0; x&lt;[arr count]; x++) {
              [self.view addSubview:[arr objectAtIndex:x]];
          }
  
  
      self.lblCount.text = [ NSString stringWithFormat:@"count: %d",count];
  
  }
  
  
  //  the delegate function for deleting the view is as follows  
  
  
  - (void) handleDelegate:(id)sender
  {
      int deletedIndex;
      for (int val=0; val&lt;[arr count]; val++) {
          if([[arr objectAtIndex:val] isEqual:sender])
              deletedIndex =val;
      }
      dupArray =[[NSMutableArray alloc]initWithArray:arr];
      [arr removeObjectAtIndex:deletedIndex];
      [sender removeFromSuperview];
      count--;
      self.lblCount.text = [ NSString stringWithFormat:@"count: %d",count];
  
  
      if ((deletedIndex%2)==0)
          i=deletedIndex/2;
      if ((deletedIndex%2)==1)
          j--;
  
  
  
  }
  </code></pre>
  
  <p><strong>method 2:</strong>      </p>
  
  <pre><code>   - (IBAction)btnAddView:(id)sender
     {
       int modifiedColValue= 10+(colValue*120);
  
          if (modifiedColValue&lt;320)
          {
  
              NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(rowValue, modifiedColValue, 100, 100) andDelegate:self];
              [arr addObject:subView];
  
              colValue++;
          }
          else if (modifiedColValue&gt;320)
          {
              rowValue=10;
              [self btnAddView:sender];
              colValue++;
          }
  
          count++;
          for (int x=0; x&lt;[arr count]; x++)
          {
              [self.view addSubview:[arr objectAtIndex:x]];
          }
  
      }
  </code></pre>
  
  <p>how can i reposition the remaining subviews,where count is the number of subviews, 320 is the width of the main view.</p>