ipalibowhyte
ipalibowhyte

Reputation: 1563

How do you hide and show a uiview when cell is selected UITableView

Basically i want to hide always hide the uiview(with buttons), until a cell is touched then it brings up the uiview from the bottom of the cell with animation(if possible)

something like this:

enter image description here

I saw this in an app and fell in love with it and thought it would be great user experience.

Please how do i archive this, cheers

code so far:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *mycell=[tableView dequeueReusableCellWithIdentifier:@"cellIdentifier"];

    if (mycell==nil) {
        mycell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellIdentifier"];
    }
    UIView *myview = [[UIView alloc] initWithFrame:CGRectMake(0, 25, 320, 50)];
    [myview setBackgroundColor:[UIColor greenColor]];
    [mycell.contentView addSubview:myview];

    mycell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
    mycell.textLabel.text = @"Song Title";
    UIFont *myFont = [ UIFont fontWithName: @"AppleSDGothicNeo-Bold" size: 12.0 ];
    [mycell.textLabel setTextColor: [UIColor colorWithRed:0.839 green:0.271 blue:0.255 alpha:1]];
    mycell.textLabel.font  = myFont;

    return mycell;
}

P.s myView is the view i want to add, which will contain the buttons . Cheers !!!

Upvotes: 0

Views: 1169

Answers (2)

Jogendra.Com
Jogendra.Com

Reputation: 6454

Try like this

  • First take a global variable

    NSInteger selectedIndex;

  • In viewDidLoad insert value in this -1;

    selectedIndex = -1;

after then use in didSelectRowAtIndexPath function insert value in this variable.. Like this and reload with animation.

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    selectedIndex = indexPath.row;
    //[tableViewObj reloadData];
    [tableViewObj reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

and in heightForRowAtIndexPath function manage height for selectedIndex using If/else like this..

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == selectedIndex) {
        return 200;
    }else{
       return 80;
    }
    return 0;
}

as I seen Have describe fully here .. try this It's working fine because It tested by me right now..

Upvotes: 0

gudatcomputers
gudatcomputers

Reputation: 2882

This option would require your UITableViewDelegate and UITableViewDataSource to be the same object, which is usually the case.

  1. In UITableViewDelegate's tableView:didSelectRowAtIndexPath:, you could manage an array of selected indexPath values, and then call [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]] for the row that was just selected/deselected.
  2. In tableView:cellForRowAtIndexPath, you can check if the current indexPath is in the list of selected indexPath managed by the UITableViewDelegate method, and take the appropriate action to show/hide the additional content

Upvotes: 1

Related Questions