1

追加ボタンで構成されるメイン ビューを作成したいと考えていました。

追加ボタンをクリックするたびに、メインビューにサブビューを追加する必要があります。サブビューは、閉じるボタンで構成される別のクラスに存在し、閉じるボタンをクリックすると、サブビュークラスはデリゲート関数と特定のサブビュー ミストはメイン ビューから削除されます。

残りのサブビューは、メイン ビューで再配置する必要があります。

メインビューにサブビューをマトリックス形式(2xN)マトリックスで追加したい。つまり、1 行に 2 つのサブビューがあり、n 列です。

同じものに対して 2 つのメソッドを実装しましたが、サブビューの位置が変更されません。

方法 1 :

    - (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<[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<[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--;



}

方法 2:

   - (IBAction)btnAddView:(id)sender
   {
     int modifiedColValue= 10+(colValue*120);

        if (modifiedColValue<320)
        {

            NewSubView *subView = [[NewSubView alloc]initWithFrame:CGRectMake(rowValue, modifiedColValue, 100, 100) andDelegate:self];
            [arr addObject:subView];

            colValue++;
        }
        else if (modifiedColValue>320)
        {
            rowValue=10;
            [self btnAddView:sender];
            colValue++;
        }

        count++;
        for (int x=0; x<[arr count]; x++)
        {
            [self.view addSubview:[arr objectAtIndex:x]];
        }

    }

残りのサブビューを再配置するにはどうすればよいですか。count はサブビューの数、320 はメイン ビューの幅です。

4

1 に答える 1

0

あなたが探している答えは UICollectionView だと思います。このチュートリアルが役に立つかもしれません: http://skeuo.com/uicollectionview-custom-layout-tutorial

于 2013-09-26T12:56:20.167 に答える