78235

Trying to expand/collapse UITableViewCell from a UIButton on the custom cell

Question:

I have a UITableView populated with custom UITableViewCells. Within those custom cells, I have a UITextField and a "See More" UIButton. The purpose of the UIButton is to dynamically expand that particular UITableCell when the user wishes to read more of the text. In the same way, when the user wishes to return to the original size, the user clicks the Button again, and the UITableViewCell will shrink to the original size.

Since the cell isn't being selected, I setup an IBAction within the Custom Cell like such:

//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]; }

Following this, 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; } }

What happens now is I can see the custom cell change the size of its textview, however, the table does not update the row height for that particular cell. Checking on the If-else statement there, it appears that hasBeenResized is always false, even though I set it to YES within the IBACtion of the CustomCell.

I have looked at other solutions here, but they all seem to involve didSelectRowAtIndexPath, which I cannot use in this instance (I have another behavior for the cell when it is selected).

Am I doing this completely wrong? Ideally, what I would like to do is to have the "Show More" button animate downwards as the textview is expanded and vice versa when it's collapsed.

Thank you!

Answer1:

Method beginUpdates won't call reloadData for you - you have to do it manually.

For your case it would be best to call:

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

And place your showMoreText code in tableView:cellForRowAtIndexPath: method (for selected cell only)

Answer2:

To Update your tableView, you have to reload it.

[tableView reloadData];

Recommend

  • How to get the links to the front when reparenting
  • Set individual onClick events in custom ViewGroup
  • Cropping a captured image exactly to how it looks in AVCaptureVideoPreviewLayer
  • How to add UIView inside a UITableViewCell Programmatically?
  • Rotating UIImageView Moves the Image Off Screen
  • GWT 2.7 super dev mode does not work with CssResource.style=obf
  • Receiver has no segue with identifier
  • Why don't added sublayers show up in screenshot?
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • Replace any string in columns with 1
  • Difficulties implementing the Hysteresis step of Canny Algorithm in Halide without define_extern fun
  • What is the difference between Socket.Send and Stream.Write? (in relation to tcp ip connections)
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • Drag and reorder - UICollectionview with sections
  • Thrust filter by key value
  • Why do GeoJSON features appear like a negative photo of the features themselves?
  • Deleting a widget from QTableView
  • cd command not working with execvp
  • Thread synchronization with syncwarp
  • react split panel resize
  • multidatatrigger with multibinding in ControlTemplate.Triggers
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Problems with toDataURL HTML5 other ways to get canvas data?
  • Find JSON nested nodes using multiple string values
  • Build Successful but not running on simulator
  • Stop Bash Script if Hive Fails
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Cannot resolve symbol 'MyApi'
  • Highlight one bar in a series in highcharts?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • VBA Convert delimiter text file to Excel
  • Adding custom controls to a full screen movie
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • -fvisibility=hidden not passed by compiler for Debug builds
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • How can I remove ASP.NET Designer.cs files?
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?