Ricardo Anjos
Ricardo Anjos

Reputation: 1427

iPhone - UILabel staying outside of the UITableViewCell

I am making a UITableViewController that resizes UITableViewCell when I select the cell.

My problem is that the content that should be hidden when the cell is not expanded (UILabel), is always visible.

What am I doing wrong? Here is my code:

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"TableViewCell";
    TableViewCell *cell = (TableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil] objectAtIndex:0];

    }
    cell.label.text = @"cenas";
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedCellIndexPath != nil && indexPath.row == self.selectedCellIndexPath.row) {
        return 200;
    }else{
        return 44;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedCellIndexPath != nil && self.selectedCellIndexPath.row == indexPath.row) {
        self.selectedCellIndexPath = nil;
    }else{
        self.selectedCellIndexPath = indexPath;
    }
    [tableView beginUpdates];
    [tableView endUpdates];
}

And here are some pictures of what is happening:

TableViewController without selected cell

UITableViewController with selected cell

Upvotes: 0

Views: 435

Answers (3)

Ricardo Anjos
Ricardo Anjos

Reputation: 1427

I found! I finally found!

I inserted inside of cellForRowAtIndexPath: the following:

[cell setClipsToBounds:YES];

This makes the content be only inside the cell

Upvotes: 2

bhargavisridharan
bhargavisridharan

Reputation: 207

In cellForRowAtIndexPath put the following code

if (self.selectedCellIndexPath != nil && indexPath.row == self.selectedCellIndexPath.row)
{
[YourLabel setHidden:NO];
}
else
{
[YourLabel setHidden:YES];    
}

Upvotes: 2

ullstrm
ullstrm

Reputation: 10160

You need to manually hide/show the labels (Preferably with animation). Just because they are not inside the cells height, it doesn't mean they are hidden.

-(void)setSelectedCellIndexPath:(NSIndexPath*)indexPath
{
    UITableViewCell *oldCell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath];

//oldCell can be nil due to possibility that it's not on screen.

   [self hideOutletsForCell:oldCell];
   _selectedIndexPath = indexPath;
    UITableViewCell *newCell = [self.tableView cellForRowAtIndexPath:_selectedIndexPath];
   [self showOutletsForCell:newCell];

}

Also, you need to reset the outlets in cellForRowAtIndexPath, and only show outlets for the indexPath that is your _selectedIndexPath

Upvotes: 1

Related Questions