Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:42:13+00:00 2026-05-23T11:42:13+00:00

I have a UITableView within a UIView The table view’s cells are custom, and

  • 0
  • I have a UITableView within a UIView
  • The table view’s cells are custom, and thus managed by a UITableViewCell class

I need to expand/contract cells upon selection, which I have done using the awesome tutorial here. However, I also need to show/hide UILabels upon selection–like a detail view, as you expand the cell, more labels are shown; contract it back to its original size, and those labels are again hidden.

Basically:
Click
Extend length of cell
Show labels

Click again
Contract cell
Hide labels

Only 1 cell open at a time

This all sounds easy enough, but the method prevalently used across the web (the tutorial I linked to above) automatically deselects its cells upon the first touch, which means my hidden UILabels never get a chance to show up.

If I remove

[tableView deselectRowAtIndexPath:indexPath animated:TRUE];

from didSelectRowAtIndexPath, I can get the hidden label to appear, but it does not disappear of course, as the cell does not get deselected until I select a different cell.

How might I make it so that the cell automatically deselects once it returns to its regular height after a user clicks it for the second time? Also, is there a way to limit the table to only one expanded cell at a time because right now, you can fully expand all cells. I’d like it if expanding Cell2 automatically shrunk Cell1 back to its original height.

Thanks all; don’t know what I’d do without Stack Overflow at times.

TableViewController.h

@interface TableViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    IBOutlet UITableView *tableView;
    NSMutableDictionary *selectedIndexes;
}
@end

Relevant code from TableViewController.m

@interface TableViewController (private)

- (BOOL)cellIsSelected:(NSIndexPath *)indexPath;

@end

@implementation TableViewController

#define kCellHeight 50.0

- (void)viewDidLoad {
    [super viewDidLoad];

    selectedIndexes = [[NSMutableDictionary alloc] init];
}


- (BOOL)cellIsSelected:(NSIndexPath *)indexPath {
    // Return whether the cell at the specified index path is selected or not
    NSNumber *selectedIndex = [selectedIndexes objectForKey:indexPath];
    return selectedIndex == nil ? FALSE : [selectedIndex boolValue];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    }

    return cell;
}

#pragma mark -
#pragma mark Tableview Delegate Methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Deselect cell
    [tableView deselectRowAtIndexPath:indexPath animated:TRUE];

    // Toggle 'selected' state
    BOOL isSelected = ![self cellIsSelected:indexPath];

    // Store cell 'selected' state keyed on indexPath
    NSNumber *selectedIndex = [NSNumber numberWithBool:isSelected];
    [selectedIndexes setObject:selectedIndex forKey:indexPath]; 

    // This is where magic happens...
    [tableView beginUpdates];
    [tableView endUpdates];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    // If our cell is selected, return double height
    if([self cellIsSelected:indexPath]) {
        return kCellHeight * 2.0;
    }

    // Cell isn't selected so return single height
    return kCellHeight;
}

@end

Also, the UILabels from the UITableCell class:

    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    if (selected == YES){
        date.hidden = NO;   
    }   
    else if (selected == NO) {
        date.hidden = YES;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:42:14+00:00Added an answer on May 23, 2026 at 11:42 am

    And I’m an idiot. The simplest solution is always the best, and this one is one line of code:

     self.contentView.clipsToBounds = YES;
    

    when you setup your cell and lay it out.

    Duh.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom view MyView : UIViewController <UITableViewDelegate, UITableViewDataSource> with a UITableView within
I have a UIScrollView contained within a custom UIView with a content size larger
i have a UITableView , within each cell i have 2 custom buttons, what
I have an initial table view that I created as the initial menu within
I have a UIViewController, and within that view i have UITableView added in IB
I'm using storyboard to create a grouped UITableView . Within the table I have
how to make a UIView added to UITableViewCell content view fit within its bounds?
I have to create a UITableView, that contains richly formatted text within each UITableViewCell.
I have a custom UITableView using UITableViewCell s. Each UITableViewCell has 2 buttons. Clicking
I have a UITableView with a Left Detail styled cell. The table view is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.