Change the Size of TableView Cells

Asked 2 years ago, Updated 2 years ago, 172 views

I would like to change the size of the cell in TableView depending on the content, but I don't know what to do.For example, suppose you have a TableView cell:

Enter a description of the image here

I would like to change the cell height according to the increasing or decreasing TextView line at the top of this cell.Is AutoLayout only possible to achieve this?
Also, you can choose whether to display or not the image under TextView, and if you do not display the image, I would like to reduce the height of the cell as much as the ImageView disappears.
What should I do to do the above?
I set my own AutoLayout, but the cell height remains the same.
Please let me know if anyone knows.
I'm sorry, but I appreciate your cooperation.

---add ---

iOS version is 9.0.1 and Xcode version is 7.0.1 and I use Storyboard.

ios objective-c uitableview autolayout

2022-09-30 18:40

2 Answers

(CGFloat) tableView:heightForRowAtIndexPath
A method that determines the height of each line, just like you read it.
tableView—Called before cellForRowAtIndexPath:

http://qiita.com/kotaroito/items/8bd2f10833e07f7a5809

Enter the code here - (CGFloat) tableView: (UITableView*) tableView heightForRowAtIndexPath: (NSIndexPath*) indexPath{
// String you want to display
NSSstring* text=_objects [indexPath.row];
// Display maximum width/height
CGSize maxSize = CGSizeMake (200, CGFLOAT_MAX);
// Font Size to Display
NSDictionary*attr=@{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0]};

// Based on the above, the size required for display
CGSize modifiedSize = [text boundingRectWithSize:maxSize]
                                         options:NSStringDrawingUsesLineFragmentOrigin
                                      attributes:attributes
                                         context:nil
                       ] .size;

// Add the upper and lower 10px margins and return the larger of 70px
return MAX(modifiedSize.height+20,70);}


2022-09-30 18:40

Perhaps TextView will not scroll.


on Storyboard - TextView does not have height constraints.Set Constraints up, down, left, and right.
- Uncheck Scrolling Enabled in TextView.
- ImageView has a height Constraint that allows custom cells to be IBOutlet properties.

The code says
- Set rowHeight and estimatedRowHeight to the table per viewDidLoaded().EstimatedRowHeight sets the approximate value for your cell.

self.tableView.rowHeight=UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight=100.0;
  • Set the ImageView height Constraint set on the Storyboard in tableView (tableView:cellForRowAtIndexPath:) to 0.0 if you do not want to see the image.

With AutoLayout alone, the height of TextView will automatically change, and if ImageView is not required, the height of the cell will be reduced.

If you want TextView scrollable and want to limit its height, you should override IntrinsicContentSize in the UITextView subclass.


2022-09-30 18:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.