从NSMutableArray xcode中删除对象

时间:2012-10-20 17:26:09

标签: ios cocoa-touch uiimageview nsmutablearray

这个想法是创建了多个对象,并且在单击按钮时将首先删除首次创建的对象,依此类推。例如,它会创建对象#1然后是2然后3,当第一次删除1,然后是2,然后是3时,单击按钮。

以下是我在数组中创建项目的代码:

//array of imageviews
aryImages= [[NSMutableArray alloc] init];
//creation code for the images
CGRect frame = CGRectMake(480, 180, 60, 55);
imgViewPicture = [[UIImageView alloc] initWithFrame:frame];
imgViewPicture.image=[UIImage imageNamed:@"flower.png"];
[self.view addSubview:imgViewPicture];

[aryImages addObject:imgViewPicture];

然后这是移动代码

for (int x = 0; x < [aryImages count]; x++) {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.5];
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    [UIView setAnimationDelegate:self];
    [imgViewPicture setFrame:CGRectMake(120, 180, 60, 55)];
    [UIView commitAnimations];
}

以下是删除特定x位置以外的项目的代码:

 for (int x = 0; x < [aryImages count]; x++) {
        UIImageView *imgViewInQuestion = (UIImageView *)[aryImages objectAtIndex:x];
        if (imgViewInQuestion.frame.origin.x>=120) {
            [aryImages removeObjectAtIndex:x];
        }
    }

不幸的是,这不起作用。任何想法为什么或可能的解决方案对我?

2 个答案:

答案 0 :(得分:1)

-(IBAction)btnDelete_Clicked:(id)sender
{
    NSInteger x=0;
    if(x <= arrImages.count)
    {
        [arrImages removeObjectAtIndex:x];
        x=x+1;
        NSLog(@"%@",arrImages);
    }
}

答案 1 :(得分:0)

非常简单 你有一个数组,例如aryImages
使用整数:

int x=0;

 -(IBAction)removeObject
    {
    if(x < = [aryImages count])
    {
        [aryImages removeObjectAtIndex:x];
        x+=1;
    }

    }