简体   繁体   中英

iOS: Moving Objects in an NSMutableArray

I have an app that behaves somewhat like a photo gallery. They choose an image from there camera roll, and the image gets displayed in a UIImageView. I have a total of 9 image views. Now, I'm trying to add in the ability to press an edit button, and allow the user to delete photos. I accomplished this by placing a hidden UIButton over each image, and when the button is tapped, a UIAlertView appears asking if they would like to delete that image. After they click "Yes" in the UIAlertView, I would like that particular UIImageView to stop displaying the picture, and move each displayed picture 1 row to the left, so that there isn't a blank space in the gallery.

This is where things get tricky for me, I'm still very new to Objective-C and have no idea how to do this. I know that I should probably call moveRowAtIndexPath and toIndexPath , but I'm not sure if I should do this in viewDidLoad, viewWillAppear, or should I just create my own method for this? Here is a sample of what I'm working with, which may or may not be relevant:

- (void)applicationDidEnterBackground:(UIApplication*)application {
    NSLog(@"Image on didenterbackground: %@", imageView);
    NSMutableArray* array = [NSMutableArray arrayWithObject:[NSData dataWithData:UIImagePNGRepresentation(imageView.image)]];

    [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView2.image)]];
     [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView3.image)]];
      [array addObject:[NSData dataWithData:UIImagePNGRepresentation(imageView4.image)]];

            [self.user setObject:array forKey:@"images"];
    [user synchronize];

            }

- (void)viewDidLoad
    {
        self.user = [NSUserDefaults standardUserDefaults];
        NSLog(@"It is %@", self.user);
        NSMutableArray* array = [[self.user objectForKey:@"images"]mutableCopy];
        imageView.image = [[UIImage alloc] initWithData:[array objectAtIndex:0]];
        imageView2.image = [[UIImage alloc] initWithData:[array objectAtIndex:1]];
        imageView3.image = [[UIImage alloc] initWithData:[array objectAtIndex:2]];
        imageView4.image = [[UIImage alloc] initWithData:[array objectAtIndex:3]];



        UIApplication *app = [UIApplication sharedApplication];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(applicationDidEnterBackground:)
                                                     name:UIApplicationDidEnterBackgroundNotification
                                                   object:app];

        backToGalleryButton.hidden = YES;
        tapToDeleteLabel.hidden = YES;
        deleteButton1.hidden = YES;
        [super viewDidLoad];

    }

Any help or advice on this is much appreciated, thank you!

Why dont you just remove the object from the array if it is a delete function? removeObject or removeObjectAtIndex will do the job just fine. If you are using a tableview then call reloadData on it next.

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