Resizing UITableView to fit content
Actually I found the answer myself.
I just create a new CGRect
for the tableView.frame
with the height
of table.contentSize.height
That sets the height of the UITableView
to the height
of its content.
Since the code modifies the UI, do not forget to run it in the main thread:
dispatch_async(dispatch_get_main_queue(), ^{
//This code will run in the main thread:
CGRect frame = self.tableView.frame;
frame.size.height = self.tableView.contentSize.height;
self.tableView.frame = frame;
});
How to resize UITableViewCell to fit its content?
You need you implement heightForRowAtIndexPath
.
Say that the data that is to be displayed in the textView is stored in a NSArray.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
CGFloat cellheight = 30; //assuming that your TextView's origin.y is 30 and TextView is the last UI element in your cell
NSString *text = (NSString *)[textArray objectAtIndex:indexpath.row];
UIFont *font = [UIFont systemFontOfSize:14];// The font should be the same as that of your textView
CGSize constraintSize = CGSizeMake(maxWidth, CGFLOAT_MAX);// maxWidth = max width for the textView
CGSize size = [text sizeWithFont:font constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
cellHeight += size.height; //you can also add a cell padding if you want some space below textView
}
Resize UITableView's height to be as high as it needs to fit only total content size
if I understand it correctly, you should be able to add up the height for each row and adjust the tableview height based on that:
CGFloat tableHeight = 0.0f;
for (int i = 0; i < [_stepsArray count]; i ++) {
tableHeight += [self tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
}
self.tableView.frame = CGRectMake(self.tableView.frame.origin.x, self.tableView.frame.origin.y, self.tableView.frame.size.width, tableHeight);
you can do this immediately after [tableView reloadData]
How to resize UITableView width to fit its content?
- Set a
width
auto-layout constraint to the table view. - Set an
outlet
from the constraint to your code (ctrl+drag, just like a view). When you have the size of the label (if the label also has auto-layout then you will have it at
viewDidLayoutSubviews
) set the width to the table like so:- (void)viewDidLayoutSubviews {
self.myTableWidthConstraint.constant = self.myLabel.frame.size.width;
[self.myTableView updateConstraints];
[self.myTableView layoutIfNeeded];
}
Resizing UITableViewCell to fit Content Performance
The way I had implemented adding the constraints, the constraints were updated whenever [self updateViewConstraints]
was called, regardless of whether or not the cell had already had its constraints calculated and applied. I resolved this by adding a BOOL property to my custom TableViewCell called didUpdateConstraints, and setting it to YES as soon as constraints are updated the first time. Then in my [self updateViewConstraints]
method I only update the constraints if !self.didUpdateConstraints
Now that constraints are not being needlessly updated when all views have already been correctly constrained, the performance is significantly better, and I don't observe any slowdown upon scrolling through the table.
How to resize UITableView with a large list to fit all the content without scrolling?
Obvious solution is to not use UITableView. You can create your contentView as normal view, either programatically or in xib, and then add it to your main view and connect it to the previous custom one using constraint. I have done it multiple times and it works like a charm.
var lastView: UIView? = nil
for item in model.items {
var customView: CustomContentView
let objects = NSBundle.mainBundle().loadNibNamed("CustomContentView", owner: self, options: nil)
customView = objects.first! as! CustomContentView
customView.translatesAutoresizingMaskIntoConstraints = false
customView.setItemModel(item)
viewWrap.addSubview(customView)
viewWrap.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[customView]|", options: [], metrics: nil, views: ["customView": customView]))
if lastView == nil {
viewWrap.addConstraint(NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: viewWrap, attribute: .Top, multiplier: 1, constant: 0))
} else {
viewWrap.addConstraint(NSLayoutConstraint(item: customView, attribute: .Top, relatedBy: .Equal, toItem: lastView!, attribute: .Bottom, multiplier: 1, constant: 0))
}
lastView = customView
}
if lastView != nil {
viewWrap.addConstraint(NSLayoutConstraint(item: viewWrap, attribute: .Bottom, relatedBy: .Equal, toItem: lastView!, attribute: .Bottom, multiplier: 1, constant: 17))
}
Then you just to make sure that your custom view can resize to fit the screen if there is too many of them. I assume you have some max limit and that they all can fit. If not, then you need to add it to scroll view.
Related Topics
String Replacement in Objective-C
Uicollectionview - Dynamic Cell Height
Storyboard Doesn't Contain a View Controller With Identifier
Swiftui | Using Ondrag and Ondrop to Reorder Items Within One Single Lazygrid
How to Build For Armv6 and Armv7 Architectures With iOS 5
Uipageviewcontroller and Storyboard
Missing Return Uitableviewcell
Retrieving Carrier Name from iPhone Programmatically
Reloaddata() of Uitableview With Dynamic Cell Heights Causes Jumpy Scrolling
How to Calculate the Size of a Folder
How to Open Settings Programmatically Like in Facebook App
Replacement For Deprecated -Sizewithfont:Constrainedtosize:Linebreakmode: in iOS 7
Getting All Cookies from Wkwebview