【问题标题】:iOS: Moving Objects in an NSMutableArrayiOS:在 NSMutableArray 中移动对象
【发布时间】:2012-04-02 09:03:22
【问题描述】:

我有一个应用程序,其行为有点像照片库。他们从相机胶卷中选择一个图像,图像显示在 UIImageView 中。我总共有 9 个图像视图。现在,我正在尝试添加按下编辑按钮的功能,并允许用户删除照片。我通过在每个图像上放置一个隐藏的 UIButton 来实现这一点,当点击该按钮时,会出现一个 UIAlertView 询问他们是否要删除该图像。在他们单击 UIAlertView 中的“是”后,我希望该特定 UIImageView 停止显示图片,并将每个显示的图片向左移动 1 行,以便画廊中没有空白区域。

这对我来说是个棘手的问题,我对 Objective-C 还是很陌生,不知道该怎么做。我知道我可能应该调用moveRowAtIndexPathtoIndexPath,但我不确定是否应该在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];

    }

非常感谢您对此提供任何帮助或建议,谢谢!

【问题讨论】:

  • 这些图像视图是表格视图吗?
  • 不,我不是,我应该有,但我有点太过分了,也回头了。不过如果有必要我会的。

标签: objective-c ios uiimageview nsmutablearray


【解决方案1】:

如果它是删除函数,为什么不直接从数组中删除对象? removeObject 或 removeObjectAtIndex 可以很好地完成这项工作。如果您使用的是 tableview,那么接下来调用 reloadData。

【讨论】:

  • 很酷,谢谢,我知道我应该使用 TableView 代替。有什么简单的方法可以将所有这些添加到 tableview 中,还是我已经太深了?如果我最终保留了我所拥有的并使用removeObjectAtIndex,是否会调用重新加载数据,以便它不会在图库中留下空白空间?对不起,新手问题,我对此还是很陌生。
  • 啊!我想可能是这样。你担心 NSMutableArray 会表现得像一个新的 Java 风格的数组,我猜。你认为如果你删除它,你的数组中就会有一个空白空间。然而,这种情况并非如此。 NSArray 不能包含 nil 作为对象,因为它以 nil 结尾,所以当你删除一个项目时,NSMutableArray 内部会移动你数组的后端。好用,嗯? :D
  • 此外,没有什么会强迫您使用表格视图。我们只是假设你是因为你写的东西(IndexPath 是 UITableView 中常用的对象,通常不会在 NSArray 中)
  • 太棒了,这是个好消息!非常感谢您的所有帮助!
  • 不,索引是大于或等于零的整数,指的是数组中的位置。如果要从可变数组中删除特定的 OBJECT,请使用 removeObject 而不是 removeObjectAtIndex。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
相关资源
最近更新 更多