2012-04-02 43 views
0

我有一個應用程序,其行爲有點像照片庫。他們從相機膠捲中選擇一張圖像,並將圖像顯示在UIImageView中。我共有9個圖像視圖。現在,我試圖添加按下編輯按鈕的功能,並允許用戶刪除照片。我通過在每個圖像上放置一個隱藏的UIButton來實現這一點,並且當點擊該按鈕時,出現一個UIAlertView,詢問他們是否想要刪除該圖像。在UIAlertView中點擊「Yes」後,我希望特定的UIImageView停止顯示圖片,並將每個顯示的圖片向左移動1行,以便圖庫中沒有空白區域。iOS:在NSMutableArray中移動對象

這是對我來說很棘手的地方,我對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]; 

    } 

任何幫助或此建議非常感謝,謝謝!

+0

這些圖像視圖在表視圖中? – 2012-04-02 00:52:05

+0

不,我不是,我應該有,但我太過分了。如果有必要,我會盡管。 – John 2012-04-02 01:06:09

回答

2

爲什麼不從數組中刪除對象,如果它是刪除函數? removeObject或removeObjectAtIndex會很好地完成這項工作。如果你正在使用tableview,那麼接下來調用reloadData。

+0

很酷謝謝你,我知道我應該用TableView來代替。有沒有簡單的方法將所有這些添加到tableview中,還是我已經太深了?如果我最終保留了我擁有的內容並使用'removeObjectAtIndex',是否有一個調用來重新加載數據,以便它不會在庫中留下空白空間?對於新手問題抱歉,我對此仍然很陌生。 – John 2012-04-02 01:11:21

+1

啊!我認爲這可能是這種情況。你擔心NSMutableArray的行爲就像我猜想的一個新的Java風格的數組。你認爲如果你刪除它,陣列中將會有一個空的空間。然而,這種情況並非如此。 NSArray不能包含nil作爲對象,因爲它以nil結尾,因此內部NSMutableArray會在刪除項目時移動數組的後端。方便,呃? :D – borrrden 2012-04-02 01:19:14

+1

此外,沒有什麼是強迫你使用表格視圖。我們只是假設你是因爲你寫了什麼(IndexPath是UITableView中常用的對象,通常不會在NSArray中) – borrrden 2012-04-02 01:20:13