Trying to expand / collapse UITableViewCell from UIButton in a custom cell

I have a UITableView populated with custom UITableViewCells. Within these user cells, I have a UITextField and UIButton Read More. The goal of UIButton is to dynamically expand this particular UITableCell when the user wants to read more text. Similarly, when the user wants to return to the original size, the user clicks the button again, and the UITableViewCell will be reduced to its original size.

Since no cell is selected, I configure IBAction inside the user cell, for example:

// Within CustomCell.m

- (IBAction)showMoreText:(id)sender
{
    //instance bool variable to flag whether the cell has been resized
    self.hasBeenResized = YES;

    //turn off mask to bounds, otherwise cell doesnt seem to resize
    [[self.cellView layer] setMasksToBounds:NO];

    // Calculate the new sizes and positions for the textView and the button 
    CGRect newTextViewFrame = self.textView.frame;
    newTextViewFrame.size.height = self.textView.contentSize.height;
    self.textView.frame = newTextViewFrame;

    CGFloat bottomYPos = self.textView.frame.origin.y + self.textView.frame.size.height;
    CGRect buttonFrame = self.showMoreButton.frame;
    buttonFrame.origin.y = bottomYPos;
    self.showMoreButton.frame = buttonFrame;

    // Call begin and end updates
    [(UITableView*) self.superview beginUpdates];   
    [(UITableView*) self.superview endUpdates];

    // Set mask and put rounded corners on the cell
    [[self.cellView layer] setMasksToBounds:YES];
    [[self.cellView layer] setCornerRadius:10.0];
}

After that, I have this in my ViewController class:

// Within ViewController.m
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"heightForRowAtIndexPath");

    CustomCell *cell = (CustomCell*)[self tableView:tableView cellForRowAtIndexPath:indexPath];

    if([cell hasBeenResized] == NO)
    {
        return cell.frame.size.height + 20;
    }
    else
    {
        return cell.frame.size.height + cell.textView.frame.origin.y + cell.textView.frame.size.height + cell.showMoreButton.frame.size.height + 20;
    }
}

, , . If-else, , hasBeenResized false, YES IBACtion CustomCell.

, , , didSelectRowAtIndexPath, ( , ).

? , , " " , .

!

+1
2

beginUpdates reloadData - .

:

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

showMoreText tableView:cellForRowAtIndexPath: ( )

+3

tableView, .

[tableView reloadData];
0

All Articles