[英]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.
他们从相机胶卷中选择一张图像,图像以UIImageView显示。 I have a total of 9 image views.
我总共有9个图像视图。 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.
我通过在每个图像上放置一个隐藏的UIButton来实现这一点,当点击按钮时,会出现一个UIAlertView,询问他们是否要删除该图像。 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.
在UIAlertView中单击“是”之后,我希望特定的UIImageView停止显示图片,并将每个显示的图片向左移动1行,这样图库中就没有空白。
This is where things get tricky for me, I'm still very new to Objective-C and have no idea how to do this. 这对我来说变得棘手,我仍然是Objective-C的新手,并且不知道如何做到这一点。 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? 我知道我应该调用
moveRowAtIndexPath
和toIndexPath
,但是我不确定我是否应该在viewDidLoad,viewWillAppear中执行此操作,还是应该为此创建自己的方法? 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.
removeObject或removeObjectAtIndex可以很好地完成工作。 If you are using a tableview then call reloadData on it next.
如果您正在使用tableview,那么请在其上调用reloadData。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.