ios - 从另一个 UIViewController 从 UITableView 中删除一个单元格

标签 ios uitableview nsmutablearray

我有两个 View Controller ,TypeViewControllerFavoritesViewController , TypeViewController包含可以通过按 UIBarButtonItem 来收藏的类型.按下按钮将类型保存到数组并将该数组保存到文件中。 FavoritesViewController打开该文件并使用其数组填充 UITableView,我在它的 ViewDidLoad 中执行此操作方法,然后在最后添加 [self.tableView reloadData]以确保 tableView 将被填充。但是,我注意到当我尝试取消收藏某个类型时(再次按 UIBarButtonItem),它不会从 tableView 中删除。我不知道我应该怎么做!如何删除 tableView 单元格?任何输入将不胜感激,如果我需要提供任何其他细节,请告诉我!谢谢=D

这是一些代码:

来自 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];
}

最佳答案

我认为您缺少的是您需要向 tableView 发送消息以实际删除该行。看起来您正在从数组中删除数据,但没有告诉已经加载数据的 tableView 删除相应的行。

在您的 tableView Controller 中使用此方法。

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

关于ios - 从另一个 UIViewController 从 UITableView 中删除一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17801735/

相关文章:

android - 如何在混合应用程序上优化静态 Assets

ios - 如何在准备要在 swift iOS 中显示的数据时制作加载页面

ios - calayer 没有根据 tableview 单元格中的 UIView 大小而改变

memory-leaks - 非ARC项目:NSMutableArray,NSString内存泄漏

ios - 推送服务证书未出现在 "My Certificates"中

iphone - View 中的 TapGestureRecognizer 从 View 禁用按钮

ios - 如何快速触发按钮选择而不是单元格选择

ios - 如何使用 NSNumbers 在 switch 语句中设置 NSDate

cocoa - 尝试插入nil

iphone - NSMutableArray 到 NSDictionary iOS 3.1.3 问题