how to hide/show a specific view inside the custom tableview cell when button click inside the same cell

Chanaka Anuradh Caldera picture Chanaka Anuradh Caldera · Sep 14, 2016 · Viewed 7.5k times · Source
  • I have a table view with (ex : 20 rows). and I have used a custom table view cell with this table view.

  • inside this table view cell, there are few labels, one button and a hidden view (UIView).

  • I have written button action for hide/show the hidden view inside the custom table view cell class.it works fine.but it affect to other rows in the table view. that means, when I tap the button in first row, then the hidden view show, and it can see in some other rows in the table view when scroll down.

  • At the same time (when hide/show), I want to increase and decrease the row height (only clicked row/cell) . what is going wrong. below is my codes and some screen shots to get an idea.

this is the first row when click in expand button

at the same time when I scroll, this is the look of other rows/cell, the show the hidden view without clicking on expand button

note : cell expand/increase it self when click on expand button in each cell.

this is how I hide and show the hidden view, inside the custom table view cell class.

- (IBAction)hideshow:(id)sender {
    BOOL ishidden = self.insideCollectionView.hidden;
    if(ishidden == true)
    {
        self.insideCollectionView.hidden = false;
    }
    else
    {
        self.insideCollectionView.hidden = true;
    }
}

what is going wrong, hope your help with this.

Advance : it is great if there is a way to do both hide/show and expand(increase the row height) of the cell when click on expand button for each cell.

Answer

Chanaka Anuradh Caldera picture Chanaka Anuradh Caldera · Sep 14, 2016

Found the suited solution for this by my self. thanx everyone who supported me.

Do the followings.

  1. Create a NSMutableArray to hold Which button in Which row clicked.
  2. Then when user click on the Button in Custom table view cell, check it that index path is already in the mutable array, if it is already inside it, then romove it ,otherwise add it.
  3. Then in the cellforrowatindexpath method, check that nsmutable array and , check whether the indexpath.row is exist or not.
  4. Finally, if it is exists, do not hide, else hide it, this works perfectly.

here is the implementation for the table view. .m file

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 25;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FirstTableViewCell *cells = [tableView dequeueReusableCellWithIdentifier:@"tvcell" forIndexPath:indexPath];

    NSString *theIndexpath = [NSString stringWithFormat:@"%ld", (long)indexPath.row];

//here check whether it is exists or not.
        if ([chekcme containsObject:theIndexpath])
        {
            cells.insideCollectionView.hidden = false;
        }
        else
        {
            cells.insideCollectionView.hidden = true;
        }



    [cells setColletionData:bbarray];

    [cells.expandMe addTarget:self action:@selector(runs:) forControlEvents:UIControlEventTouchUpInside];

//in here set the tag of the button to indexpath.row
    cells.expandMe.tag = indexPath.row;


    return cells;
}

//this is the action for the button inside the custom tableview cell.
- (IBAction)runs:(UIButton *)sender{

    NSString *myob = [NSString stringWithFormat:@"%li", (long)sender.tag];
    NSLog(@"%@",myob);

    if ([chekcme containsObject:myob]) {
        [chekcme removeObject:myob];
    }
    else
    {
        [chekcme addObject:myob];
    }


    NSLog(@"%@", chekcme);

//keep in mind to reload the table view here.
    [self.maintableView reloadData];
}

note : checkme is NSMutableArray to holds the objects clicked by the user.