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:
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
(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);}
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;
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.
617 Uncaught (inpromise) Error on Electron: An object could not be cloned
912 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
574 Who developed the "avformat-59.dll" that comes with FFmpeg?
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.