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

时间:2016-06-16 03:37:35

标签: 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;
}

1 个答案:

答案 0 :(得分:1)

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

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

[[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];
}