ios - 如何从另一个 AMSlideMenuMainViewController 中删除 UITableView 的 AllObjects NSMutableArray?

标签 ios xcode7

我有 AMSlideMenuMainViewController 和 GioHangTableController。当我从 AMSlideMenuMainViewController 单击按钮注销时,我想从 GioHangTableController 中删除 UITableView 的 AllObjects NSMutableArray。我试着打电话:

- (void)removeArrayObjects
{
    [cartListing removeAllObjects];
    [self.tableView reloadData];
}

我的GioHangTableController 函数。我想从 AMSlideMenuMainViewController 调用它。请帮我解决问题?

GioHangTableController.m

@interface GioHangTableController ()
@end

@implementation GioHangTableController
@synthesize cartListing;
UITableView *tableView;

- (NSManagedObjectContext *)managedObjectContext
{
    NSManagedObjectContext *context= nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if([delegate performSelector:@selector(managedObjectContext)]){
        context = [delegate managedObjectContext];
    }
    return context;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [cartListing removeAllObjects];
    [self.tableView reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(receiveTestNotification:)
                                                 name:@"ReloadNotification"
                                               object:nil];;

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return cartListing.count;
}
NSInteger borderThickness = 1;

- (UITableViewCell *)tableView:tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadCurrency= [defaults objectForKey:@"currency"];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellCart" forIndexPath:indexPath];
    //self.navigationItem.leftBarButtonItem = self.editButtonItem;

    UIView *bottomBorderdt = [UIView new];
    bottomBorderdt.backgroundColor = [UIColor colorWithHue:0.1 saturation:0.1 brightness:0.1 alpha:0.1];
    bottomBorderdt.frame = CGRectMake(0, cell.frame.size.height - borderThickness, cell.frame.size.width, borderThickness);
    [cell addSubview:bottomBorderdt];

    CartItems *info = [cartListing objectAtIndex:indexPath.row];

    [cell.textLabel setText:[NSString stringWithFormat:@"%@", info.name]];

    [cell.detailTextLabel setText:[NSString stringWithFormat:@"%@ X %@(%@)", [info.quantity stringValue], [NSNumberFormatter localizedStringFromNumber:info.price numberStyle:NSNumberFormatterDecimalStyle], loadCurrency]];
    return cell;
}
- (NSString *)tableView:tableView titleForHeaderInSection:(NSInteger)section{
    NSString *sectionName;
    if(section == 0){
        sectionName = @"Chi Tiết Giỏ Hàng";
    }
    return sectionName;
}

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}

UIAlertController * alertController;

// Override to support editing the table view.
- (void)tableView:tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *loadCurrency= [defaults objectForKey:@"currency"];

    NSManagedObjectContext *context = [self managedObjectContext];
    CartItems *info = [cartListing objectAtIndex:indexPath.row];

    NSString *chitiet=[NSString stringWithFormat:@"Thẻ: %@ \n Số Lượng: %@ X %@(%@)",info.name, [info.quantity stringValue], [NSNumberFormatter localizedStringFromNumber:info.price numberStyle:NSNumberFormatterDecimalStyle],loadCurrency];

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        //NSLog(@"san pham%@",info.name) ;

        alertController = [UIAlertController alertControllerWithTitle: @"Chi Tiết Bạn Muốn Xoá?"
                                                              message: chitiet
                                                       preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Huỷ" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self.tableView reloadData];

        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"Xác Nhận" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [context deleteObject:[cartListing objectAtIndex:indexPath.row]];
            NSError *error= nil;
            if(![context save:&error]){
                NSLog(@"Can't delete! %@ %@", error, [error localizedDescription]);
                return;
            }else
            {
                //[cartListing removeAllObjects];
                [self removeArrayObjects];


                /*[cartListing removeObjectAtIndex:indexPath.row];
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];*/

            }

        }]];

        [self presentViewController:alertController animated:YES completion:nil];

        //[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
}

- (void)removeArrayObjects
{
    [cartListing removeAllObjects];
    [self.tableView reloadData];
}
- (void) receiveTestNotification:(NSNotification *) notification
{
    [cartListing removeAllObjects];
    [self.tableView reloadData];
}


-(NSString *)tableView:tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"Xoá";
}

@end

注销按钮

- (NSString *)segueIdentifierForIndexPathInLeftMenu:(NSIndexPath *)indexPath
{


    NSString *identifier;
    switch(indexPath.row){
        case 0:
            identifier = @"trangchuSeque";
            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"ReloadNotification"
             object:self];
            break;
        case 1:
            identifier = @"thegameSegue";
            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"ReloadNotification"
             object:self];
            break;
        case 2:
            identifier = @"thebitSegue";
            break;
        case 3:
            identifier = @"themobaySeque";
            break;
        case 4:
            identifier = @"thezingcardSeque";
            break;
        case 5:
            identifier = @"gateSeque";
            break;
        case 6:
            identifier = @"vcoinSeque";
            break;
        case 7:
            identifier = @"garenaSeque";
            break;
        case 8:
            identifier = @"oncashSeque";
            break;
        case 9:
            identifier = @"thedienthoaiSegue";
            break;
        case 10:
            identifier = @"themobilphoneSeque";
            break;
        case 11:
            identifier = @"thevinaphoneSeque";
            break;
        case 12:
            identifier = @"theviettelSeque";
            break;
        case 13:
            identifier = @"lienheSegue";
            break;
        case 14:
            [[NSNotificationCenter defaultCenter]
             postNotificationName:@"ReloadNotification"
             object:self];
            [self showUIAlertWithMessage:@"Đăng xuất thành công." andTitle:@"Thông Báo"];
            NSString *domainName = [[NSBundle mainBundle] bundleIdentifier];
            [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:domainName];
            FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
            [loginManager logOut];            



            break;
    }
    return identifier;
}

最佳答案

您可以使用委托(delegate)方法或 NSNotification 将事件从一个类触发到另一个类。您必须详细研究它们,但作为快速修复,您可以使用以下代码。

在注销按钮的action中,添加以下代码

[[NSNotificationCenter defaultCenter] 
    postNotificationName:@"ReloadNotification" 
    object:self];

在你的 GioHangTableController 中,

-(void)viewDidLoad
{
   [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(receiveTestNotification:) 
    name:@"ReloadNotification"
    object:nil];;
}

 - (void) receiveTestNotification:(NSNotification *) notification
{
    [cartListing removeAllObjects];
    [self.tableView reloadData];
}

关于ios - 如何从另一个 AMSlideMenuMainViewController 中删除 UITableView 的 AllObjects NSMutableArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849476/

相关文章:

ios - 如何将 UDID 添加到 Xcode Managed Provisioning Profile

c++ - Opencv 3.0 features2d.hpp错误: unknown AlgorithmInfo

ios - 选择单元格后如何更改UITableViewCell图像?

ios - UIImagePicker cameraOverlayView 出现在重拍屏幕上

ios - 使用 JSON 格式从网站获取数据

ios - 书 "Beginning iOS 9 Programming with Swift"中的堆栈 View 示例

ios - 架构 armv7 的 undefined symbol : cocoaPods iPhone 5

ios - Xcode 7.3.1 在 MacOS Sierra Beta 5 上崩溃

ios - 如何使用自动布局动态添加 View

ios - Xcode 4.4找不到此类文件正在启动可执行文件