0

I have built a sample expandable tableview project. Everything is working fine. Now I want to minimize the expanded cell when I tap on the same cell for the second time. Can anyone give me any idea how can I do that? Any help is highly appreciated.

This is my code

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    NSMutableArray *arrSelected;
    NSIndexPath *selectedRowIndex;
    BOOL *isTwoTaps;

}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.arrFields = @[@"Personal Info", @"Education", @"Hobbies", @"Interest"];
    self.arrPersonalInfo = @[@"First Name", @"Last Name", @"Age", @"Phone Number",@"Address"];
    self.arrEducation = @[@"10", @"10+2", @"Graduation", @"Masters", @"Higher Studies"];
    self.arrHobbies = @[@"Sports", @"Cooking", @"Music", @"Gardening", @"Fishing"];
    self.arrInterest = @[@"Collection", @"Space", @"Medical", @"Engineering", @"Programming"];
    [self.tableView setAllowsMultipleSelection:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrFields count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier=@"CellA";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    UIView *vw=(UIView *)[cell.contentView viewWithTag:999];
    UILabel *lblField = (UILabel *)[cell.contentView viewWithTag:1];
    lblField.text = [self.arrFields objectAtIndex:indexPath.row];

    UILabel *lblDetail1 = (UILabel *)[cell.contentView viewWithTag:2];
    UILabel *lblDetail2 = (UILabel *)[cell.contentView viewWithTag:3];
    UILabel *lblDetail3 = (UILabel *)[cell.contentView viewWithTag:4];
    UILabel *lblDetail4 = (UILabel *)[cell.contentView viewWithTag:5];
    UILabel *lblDetail5 = (UILabel *)[cell.contentView viewWithTag:6];


    if([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Personal Info"]) {

        lblDetail1.text = [self.arrPersonalInfo objectAtIndex:indexPath.row];

        lblDetail2.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+1];

        lblDetail3.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+2];

        lblDetail4.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+3];

        lblDetail5.text = [self.arrPersonalInfo objectAtIndex:indexPath.row+4];

        NSLog(@"%@", [self.arrFields objectAtIndex:indexPath.row]);


    }
    else if ([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Education"]){

        lblDetail1.text = [self.arrEducation objectAtIndex:indexPath.row-1];

        lblDetail2.text = [self.arrEducation objectAtIndex:indexPath.row];

        lblDetail3.text = [self.arrEducation objectAtIndex:indexPath.row+1];

        lblDetail4.text = [self.arrEducation objectAtIndex:indexPath.row+2];

        lblDetail5.text = [self.arrEducation objectAtIndex:indexPath.row+3];

    }
    else if ([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Hobbies"]){

        lblDetail1.text = [self.arrHobbies objectAtIndex:indexPath.row-2];

        lblDetail2.text = [self.arrHobbies objectAtIndex:indexPath.row-1];

        lblDetail3.text = [self.arrHobbies objectAtIndex:indexPath.row];

        lblDetail4.text = [self.arrHobbies objectAtIndex:indexPath.row+1];

        lblDetail5.text = [self.arrHobbies objectAtIndex:indexPath.row+2];

   }
    else if([[self.arrFields objectAtIndex:indexPath.row]isEqualToString:@"Interest"]){

        lblDetail1.text = [self.arrInterest objectAtIndex:indexPath.row-3];

       lblDetail2.text = [self.arrInterest objectAtIndex:indexPath.row-2];

       lblDetail3.text = [self.arrInterest objectAtIndex:indexPath.row-1];

       lblDetail4.text = [self.arrInterest objectAtIndex:indexPath.row];

        lblDetail5.text = [self.arrInterest objectAtIndex:indexPath.row+1];

}

   if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {

         vw.hidden=FALSE;
    }
    else
   {
      vw.hidden=TRUE;
    }
    return cell;

}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  if(selectedRowIndex)
   {
       [tableView reloadData];
   }


    selectedRowIndex = indexPath ;

    [tableView beginUpdates];

    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    [tableView endUpdates];

}

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if(selectedRowIndex && indexPath.row == selectedRowIndex.row) {

        return 222;

    }
    else{

        return 44;

    }

}
iPeter
  • 1,330
  • 10
  • 26
  • you need to use uitapgesturerecognizer and need to set tap count to 2.take a look at http://stackoverflow.com/questions/8876202/uitapgesturerecognizer-single-tap-and-double-tap – Akash Shinde Apr 09 '15 at 06:11
  • Can't I do that without uitapgesturerecognizer and only by using the method didSelectRowAtIndexPath? @AkashShinde – iPeter Apr 09 '15 at 06:13
  • you can use didSelectRowAtIndexPath,when you click on cell this delegate method will get called,so under this method put uitapgesturerecognizer code,and it recognises double tap it will expand. – Akash Shinde Apr 09 '15 at 06:20

2 Answers2

1

Try this

@implementation ViewController
{
    NSMutableArray *arrSelected;
    NSIndexPath *selectedRowIndex;
    BOOL *isTwoTaps;
    NSInteger selectedIndex;

}

 - (void)viewDidLoad {
     [super viewDidLoad];

    selectedIndex=-1;
  }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{

  if(selectedRowIndex)
  {
       [tableView reloadData];
  }


  selectedRowIndex = indexPath ;
  if (selectedIndex==indexPath.row)
  {
      selectedIndex=-1;
  }
  else
 {
      selectedIndex=indexPath.row;
 }

[tableView beginUpdates];

[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

[tableView endUpdates];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

     if(selectedIndex==indexPath.row) {

        return 222;

    }
    else{

        return 44;

     }

 }
user4261201
  • 2,324
  • 19
  • 26
  • Not minimizing. :( @Nandu – iPeter Apr 09 '15 at 06:35
  • Dude its working fine. @Nandu Just want to know two things from you. Firstly, the vw is not hiding, and as a result after minimizing the cells are not visible. Secondly, if you can just tell me what have you done exactly. I just want to know the logic. – iPeter Apr 09 '15 at 07:01
  • 1
    Access the vw in didselect method and play with that accordingly. I have taken one NSInteger variable selectedIndex and assigned it's value as "-1" in viewDidLoad. When you selected the cell i am checking the selected index with the variable accordingly. When the cell is selected 1st time, i am assigning the selected row index to the variable "selectedIndex" and when you again selected the same cell , i am assigning the "selectedIndex" to -1. And in heightForRowAtIndexPath i am setting the height of cell based on that variable only. – user4261201 Apr 09 '15 at 09:34
0

Take a Boolean variable like isCellExpanded, make that value true for the first time click, when user clicks it again make it false.

Sivajee Battina
  • 4,124
  • 2
  • 22
  • 45
  • I understood your logic but how to know if user tapping for the second time? More clearly where to make the Boolean false? @SIVAJEE – iPeter Apr 09 '15 at 06:26
  • Use it in didSelectRowAtIndexPath. Like this: if (isExapndableListOpen) { isExapndableListOpen=false; } – Sivajee Battina Apr 09 '15 at 06:44
  • I am not getting your point. Firstly, when the bool will true and when will false. It will be great if you can plz post some code. Secondly, when the bool is false, then do I need to write any code to minimize the cell? @SIVAJEE – iPeter Apr 09 '15 at 06:52