2013-07-23 98 views
2

我有兩個視圖控制器,TypeViewControllerFavoritesViewControllerTypeViewController包含一個可以通過按UIBarButtonItem來收藏的類型。按下按鈕將該類型保存到數組,並將該數組保存到文件中。 FavoritesViewController打開該文件並使用其數組填充UITableView,我在其中使用ViewDidLoad方法,然後在最後添加[self.tableView reloadData]以確保tableView將被填充。但是,我注意到當我嘗試不喜歡某個類型時(通過再次按UIBarButtonItem),它不會從tableView中刪除。我不知道我應該如何去做這件事!我怎樣才能刪除tableView單元格?任何輸入將不勝感激,請讓我知道,如果我需要給任何其他細節!由於= d從另一個UIViewController中從UITableView中刪除一個單元格


下面是一些代碼:

TypeViewController

//When The UIBarButtonItem is pressed 
-(IBAction)favoriteTheSubject:(id)sender{ 

//Open the saved array 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *arrayFileName = [documentsDirectory stringByAppendingPathComponent:@"favoritedTypes.dat"]; 

NSMutableArray *favoritedTypeInfo = [[NSMutableArray alloc] initWithContentsOfFile: arrayFileName]; 
//If the array file does not exist, set the array 
if(favoritedTypeInfo == nil) 
{ 
    NSLog(@"Didn't exist in SecondaryDetailVC"); 
    favoritedTypeInfo = [[NSMutableArray alloc] init]; 
} 
//Favorite item 
if ([self.navigationItem.rightBarButtonItem.image isEqual:[UIImage imageNamed:@"FavButton.png" ]]) { //FavButton.png is the unselected favorite button 
    [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"FavButtonSelected.png"]];//Change the image 
    //Add the type 
    [favoritedTypeInfo addObject:typeOfObject]; 
    [favoritedTypeInfo addObject:priceOfType]; 

    NSLog(@"Added Item"); 

    [favoritedTypeInfo writeToFile:arrayFileName atomically:YES]; 
} 
else{ //Unfavorite Item 
    [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"FavButton.png"]]; 
    //Create an array to insert all the values to remove 
    NSMutableArray *removeArray = [[NSMutableArray alloc]init]; 

    for (int i = 0; i < [favoritedTypeInfo count]; i++) { 
     NSString *sameTitle =favoritedTypeInfo[i]; 

     if ([self.tableTitle isEqualToString:sameTitle]) { 
      //Add typeOfObject and priceOfType to removeArray 
      [removeArray addObject:favoritedTypeInfo[i]]; 
      [removeArray addObject:favoritedTypeInfo[i+1]]; 
      NSLog(@"Removed %@", removeArray[i]); 
     } 
    } 
    //Remove objects 
    [favoritedTypeInfo removeObjectsInArray:removeArray]; 
    [favoritedTypeInfo writeToFile:arrayFileName atomically:YES]; 
} 
} 

FavoriteViewController

-(void)viewWillAppear:(BOOL)animated{ 
[super viewWillAppear:animated]; 
//Open the array from file 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

NSString *arrayFileName = [documentsDirectory stringByAppendingPathComponent:@"favoritedTypes.dat"]; 

NSMutableArray *savedArray = [[NSMutableArray alloc] initWithContentsOfFile: arrayFileName]; 
if(savedArray == nil) 
{ 
    NSLog(@"Couldn't Open Favorites Array"); 
}else{ 

    for (int i = 0; i < [savedArray count]; i++) { 
     if (i%2 == 0) { 
      //Array for favorited typesOfObject 
      [self.favoritedTypesInfo addObject:savedArray[i]]; 
     } else if (i%2 == 1){ 
      //Array for favorited priceOftypes 
      [self.favoritedPriceTypes addObject:savedArray[i]]; 
     } 
    } 

    //Test to see if there are duplicate objects in arrays 
    int countFavorited = 0; 
    for (int i = 0; i < [self.favoritedTypesInfo count]; i++) { 
     for (int k = 0; k < [self.favoritedTypesInfo count]; k++) { 
      NSString *kString = [self.favoritedTypesInfo objectAtIndex:k]; 
      NSString *iString = [self.favoritedTypesInfo objectAtIndex:i]; 
      if ([kString isEqualToString:iString] && kString.length == iString.length) { 
       NSLog(@"%@ : %@", kString, iString); 
       countFavorited += 1; 
       if (countFavorited > 1) { 
        [self.favoritedTypesInfo removeObjectAtIndex:k]; 
        [self.favoritedPriceTypes removeObjectAtIndex:k]; 
        countFavorited = 0; 
       } 
      } 
     } 
    } 
} 

[self.tableView reloadData]; 
} 
+0

您是否在數組中刪除該類型? –

+0

是的!我在刪除它之後重新將它寫入文件。我忘了補充,如果我關閉我的應用程序,然後在xcode上重新運行它,或者如果我完全停止它的過程,它會更新表格,但我希望它立即更新表格 –

+2

顯示一些代碼,以便某人可以告訴你實際問題在哪裏? –

回答

0

我嘗試了deleteRowsAtIndexPath,但它會不斷地給我一個錯誤我不明白,但我發現修復,這是非常容易的。我所要做的就是初始化我的self.favoritedTypesInfo陣列FavoritesViewController,viewWillLoad而不是viewDidLoad。這樣,數組將繼續初始化而不存在任何對象,並且保存到文件中的當前位於數組中的所有對象將被放置在self.favoritedTypesInfo陣列中。

感謝所有的幫助傢伙!

0

我認爲你缺少的是,你需要發送消息到你的tableView去實際刪除該行。它看起來像從數組中刪除數據,但不告訴已經加載數據的tableView刪除相應的行。

在您的tableView控制器中使用此方法。

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
相關問題