[英]iOS: Moving Objects in an NSMutableArray
我的应用程序有点像照片库。 他们从相机胶卷中选择一张图像,图像以UIImageView显示。 我总共有9个图像视图。 现在,我正在尝试添加按下编辑按钮的功能,并允许用户删除照片。 我通过在每个图像上放置一个隐藏的UIButton来实现这一点,当点击按钮时,会出现一个UIAlertView,询问他们是否要删除该图像。 在UIAlertView中单击“是”之后,我希望特定的UIImageView停止显示图片,并将每个显示的图片向左移动1行,这样图库中就没有空白。
这对我来说变得棘手,我仍然是Objective-C的新手,并且不知道如何做到这一点。 我知道我应该调用moveRowAtIndexPath
和toIndexPath
,但是我不确定我是否应该在viewDidLoad,viewWillAppear中执行此操作,还是应该为此创建自己的方法? 以下是我正在使用的示例,其中可能相关或不相关:
- (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];
}
对此有任何帮助或建议非常感谢,谢谢!
如果它是删除函数,为什么不从数组中删除该对象? removeObject或removeObjectAtIndex可以很好地完成工作。 如果您正在使用tableview,那么请在其上调用reloadData。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.