oscarimonbox
7/26/2016 - 8:11 AM

tableview con cabecera y detalle que se esconde

tableview con cabecera y detalle que se esconde

1) LO QUE SE ESCONDE DEBE SER TABLECELL
2) LA CABECERA DEBE SER UIVIEW


//
//  EUContactListController.m
//

#import "EUContactListController.h"
#import "ApplicationModel.h"
#import "EUContactCentersHeaderView.h"
#import "EUContactCentersCell.h"
#import "EUContact.h"


@interface EUContactListController (){
    NSMutableDictionary* hshExpandedSection;
}
@end

@implementation EUContactListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    hshExpandedSection = [NSMutableDictionary new];
    
    handle_tap(self.imgBack, self, @selector(goBack:));
    
    //Asignamos DataSource y Delegate
    _tableView.dataSource=self;
    _tableView.delegate=self;
    
    //Asociamos el XIB que incrustaremos
  //  [self.tableView registerClass:EUContactCentersHeaderView.class forCellReuseIdentifier:@"EUContactCentersHeaderView"];
  //  [self.tableView registerNib:[UINib nibWithNibName:@"EUContactCentersHeaderView" bundle:nil] forCellReuseIdentifier:@"EUContactCentersHeaderView"];
    
    [self.tableView registerClass:EUContactCentersCell.class forCellReuseIdentifier:@"EUContactCentersCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"EUContactCentersCell" bundle:nil] forCellReuseIdentifier:@"EUContactCentersCell"];
    
    //Quitamos separador
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
}

-(void) goBack: (UITapGestureRecognizer*) sender{
   
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
-(void) localizeStrings{
    self.lblTitle.text = NSLocalizedString(@"contact", nil);
}

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


-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [[ApplicationModel sharedInstance].centersList count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if([hshExpandedSection objectForKey:[NSNumber numberWithInt:(int) section]]){
        return 1;
    }
    
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    EUContactCentersHeaderView * h = [[EUContactCentersHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, CONTACT_HEADER_HEIGHT)];
    
    h.tag = section;
    handle_tap(h, self, @selector(showContactInfo:));
    
    
    EUContact* contact = [[ApplicationModel sharedInstance].centersList objectAtIndex:section];
    
    h.lblCenterName.text = contact.centerName;

    
    if([hshExpandedSection objectForKey:[NSNumber numberWithInt:(int) section]]){
        [h.vwSeparator setHidden:YES];
    }
    else{
        [h.vwSeparator setHidden:NO];
    }
    
    
    return h;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return CONTACT_HEIGHT;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EUContactCentersCell *detailCell;
    
    EUContact* contact = [[ApplicationModel sharedInstance].centersList objectAtIndex:indexPath.row];
    
    NSString *cellIdentifier = @"EUContactCentersCell";
    detailCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    detailCell.tag = indexPath.section;
    
    [detailCell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    detailCell.lblPhone.text = contact.centerPhone;
    detailCell.lblFax.text = contact.centerFax;
    detailCell.lblEmail.text = contact.centerEmail;
    detailCell.lblAddress.text = contact.centerAddress;
    
    handle_tap(detailCell, self, @selector(showContactInfo:));
    
    return detailCell;
}

-(void)showContactInfo: (UITapGestureRecognizer*) sender{
    
    NSNumber* section =[NSNumber numberWithInteger:sender.view.tag];
    
    if([hshExpandedSection objectForKey:section]){
        [hshExpandedSection removeObjectForKey:section];
    }
    else{
        [hshExpandedSection setObject:section forKey:section];
    }
    
    [self.tableView reloadData];

}

@end