Get height of each rows of UITableView - ios

Is there any way by which I can get row height of each row in a UITableView. I have a UITableView which contains data which is dynamic, and I am using
self.tableView.rowHeight = UITableViewAutomaticDimension
to make dynamic row heights.
But I want to change height of overall UITableView as well, for that I need height of all the rows after reload of data.
I tried using
self.tableView.layoutIfNeeded()
self.tableViewHeightContraint.constant = self.tableView.contentSize.height
But due to unknown reason it is giving me height less than what exactly it has, after I add 3 or 4 records in UITableView so thinking of other way around. To calculate height of each rows and then summing them up.
I am doing in Xcode 7.2, iOS 9 and Swift 2

var dictionaryOfCellHeight = [Int : CGFloat]()
var dataArray = [String]()
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if dictionaryOfCellHeight[indexPath.row] == nil
{
var frame : CGRect = tableView.rectForRowAtIndexPath(indexPath)
if indexPath.row == dataArray.count
{
dictionaryOfCellHeight[indexPath.row] = frame.size.height
//calculate total height and reload table
}
else
{
dictionaryOfCellHeight[indexPath.row] = frame.size.height
}
}
}

in Swift 3.0
let myRowHeight = yourTableView.rowHeight

To change the height of Tableview , you don't need to calculate the height of all UITableViewCells and set to TableView.
Set the height of each UITableViewCell properly in heightForRowAtIndexPath method. This will automatically set the scrollable height to your Tableview

You can use this
CGRect frame = [tableView rectForRowAtIndexPath:indexPath];
NSLog(#"row height : %f", frame.size.height);
Using frame.size.height you can get height of particular row

This is because self.tableView.contentSize.height returns number of rows * estimatedRowHeight, which is not equivalent to actual height of the table view. What you need is to get the individual cell heights through visibleCells, then sum those up to get the table view's height.
Refer to this for the answer:
https://stackoverflow.com/a/40081129/4076956

Maybe the table is not resizing its content, so if you try using [self. tableView layoutSubviews]; to force the resizing, it may work. [self. tableView layoutIfNeeded]; not necessarily forces the update.

Functionally (one-liner) 👌
NOTE: my section array stores the rows in the .data var
let heightOfAllRows:CGFloat = sections.enumerated().map { arg -> [(section:Int,row:Int)] in
return arg.element.data.indices.map{ i in
(section:arg.offset,row:i)
}
}.flatMap{$0}.reduce(0){
return $0 + self.tableView(self, heightForRowAt: .init(row: $1.row, section: $1.section))
}

Related

UITableView Alignment with UITextView

I am trying to make a UITableView line up with the height sizing of paragraphs in a UITextView. Example: The timestamps to the left are what I am trying to do. I changed my code to use UIView's instead of TVcells to see what was wrong and you can see the orange view is overlapping the cyan one, meaning that the views don't actually line up but they overlap. NOTE: I am wanting to use the TableView not UIView's I am having trouble understanding how the text heights are calculated in iOS. I am using the below code to get the heights of each paragraph:
let liveParagraphView = textView.selectionRects(for: txtRange).reduce(CGRect.null) { $0.union($1.rect) }
After this I calculate the height of each then feed that into my UITableView heightForRowAt
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let models = getParagraphModel()
let height = models[indexPath.row].height
let finalHeight = models[indexPath.row].height
let heightValue = finalHeight
return CGFloat(heightValue);
}
Every line has different height values but even when using these values it's not lining up. The problem seems to be that every line calculates a Y Position which is not directly under the line before it. It's ON TOP OF!! Resulting in the UITableView not being alined when new cells are added and that 'overlay' of the selectionRects isn't taken into account. Am I correct by this? How could I go about achieving this?
Swift 5
Firstly you should set your textView (which is in the cell) dynamic height:
textView.translatesAutoresizingMaskIntoConstraints = true
textView.sizeToFit()
textView.isScrollEnabled = false
Then calculate your textView's number of lines in textDidChange etc. for update tableView's layout.
let numOfLines = (yourTextView.contentSize.height / yourTextView.font.lineHeight) as? Int
When textView's text one line down you should update tableView layout:
tableView.beginUpdates()
tableView.endUpdates()
And then you should set your tableView cell's intrinsicContentSize for dynamic rowHeight:
Set your cell's (which is the contains textView) layout without static height,
Set your tableView's rowHeight and estimatedRowHeight:
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = 44 // whatever you want
So now you have tableView cell with dynamicHeight

TableView Automatic Dimension Tableview Inside Tableview

I have Tableview for showing food orders placed by clients. which contains Horizontal UIStackview. In UIStackView
In UIStackview There is few one / two lined label and one UITableView which is used to show orderItems. Inner tableview has fixed height constant (greater then or equal + 750 Priority) and will be
changed with content size. height and scrolling disabled.
inner tableview has two labels. and heigh is automatic Dimension
I want main tableview cell height should be increased automatically as per inner tableview items. so I apply to main tableview
self.tblOrders.rowHeight = UITableViewAutomaticDimension
self.tblOrders.estimatedRowHeight = 45
and In tableview cell subclass
override func awakeFromNib() {
super.awakeFromNib()
self.tblOrderMaster.dataSource = self
self.tblOrderMaster.delegate = self
self.tblOrderMaster.estimatedRowHeight = 45
self.tblOrderMaster.rowHeight = UITableViewAutomaticDimension
self.selectionStyle = .none
self.layoutIfNeeded()
self.tblOrderMaster.isScrollEnabled = false
}
Inner tableview datasource array
var orderData:[ODOrderDataMaster] = [] {
didSet {
self.tblOrderMaster.reloadData()
self.layoutIfNeeded()
self.const_Height_tblOrderMaster.constant = self.tblOrderMaster.contentSize.height
self.layoutIfNeeded()
}
}
Question: When I scroll main tableview it is not correctly resized as per subitems.while initially it is showing correct.
I have almost tried everything like layoutIfNeeded. layoutSubviews. setNeedsDisplay.
I have also implemented willDisplayCell but not success.
I think issue is when I reload inner tableview. and when it is finished reload how can i inform to main tableview and how can i update height constant of inner tableview
Please let me know if anyone need more info.
I appreciate any help or suggestion
Add these 2 lines before the end of cellForRowAt in main & inner tableViews
cell.layoutSubviews()
cell.layoutIfNeeded()
return cell
Also you may try add layoutSubviews method in main cell subclass and do this
self.tblOrderMaster.layoutIfNeeded()
and in viewDidLayoutSubviews of controller of the main tableView and do this
self.tblOrders.layoutIfNeeded()
Try adding this to the tblOrders :
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
cell.contentView.setNeedsLayout()
cell.contentView.layoutIfNeeded()
}
Why don't you do it based on the rowcount of the inner tableView for example let's say your inner tableview has a rowheight of 30 points than to set the parenttableview rowheight all you have to do is to multiply the rowcount of the child tableview by the rowheight and you will get exactly what you want.
I have tried almost all the possible solutions but each has some problems. So I have found another workaround to fix this It can not be called perfect solution but yes it works for me
In tableview cell, I have added One Hidden Label which is Top : 0 , Bottom : 0 , left : 0 and right : 0
Now
// This is temp label which help to calulate height to tableview
if let orderMaster = restObj.orderDataMaster {
var tempText = ""
for orderMasterObject in orderMaster {
tempText += (orderMasterObject.item?.name ?? "") + "\n" + " "
if let subItems = orderMasterObject.masterSubitem {
let result = subItems.reduce("", { (result, subItem) -> String in
return result + (subItem.subitem?.name ?? "") + ","
})
tempText += result + "\n" + " "
}
tempText += "\n"
}
cell.lblTemp.text = tempText
}
self.view.layoutIfNeeded()
cell.setNeedsLayout()
cell.delegate = self
What here done is, Inner tableview's cell has labels which grow as per text. So That temp label has same text (masterSubitem) in my case, in short I have replicated inner tableview cell in that label as we have provided All Four constrains to label so when it will grow our tableview cell will grow along with it.
Hope it is helpful to someone who is facing the same issue If any of you can post another working solution I will happily accept it :)

UILabel inside UITableViewCell - height isn't adjusting correctly

Check out the second UILabel inside of the pink UITableViewCell. As you can see, the text inside it is cut-off. I've been trying to get the height of this UILabel to correctly expand or shrink as a function of how much text is plugged into it - and its not working.
• I'm using AutoLayouts
• The UILabel is pinned on all 4 sides to the ContentView - which of course is inside the UITableViewCell
• The Cells are all Static cells by the way, not Prototype cells.
• Here's the code I'm using:
(in viewWillAppear)
descriptionLabel.text = " < a bunch of text goes here > "
descriptionLabelSize = descriptionLabel.sizeThatFits(descriptionLabel.bounds.size)
// here's a global var I use to store the height:
descriptionLabelHeight = descriptionLabelSize.height
then, in viewDidAppear (and FYI, I also tried putting the following in will and didLayoutSubviews and in all sorts of other permutations:)
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
var newFrame = descriptionLabel.frame
newFrame.size.height = descriptionLabelHeight
descriptionLabel.frame = newFrame
descriptionLabel.setNeedsLayout()
view.layoutIfNeeded()
}
Finally, in heightForRowAtIndexPath I use the global var to adjust the TableViewCell's height:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch indexPath.row {
case 0: return 50
case 1: return descriptionLabelHeight+20 // the + 20 is for padding & margin
default: return 50
}
}
The result is mixed: the height of the Label - and the Cell housing it - does adjust - but not enough. It always ends up being too short. And I don't mean that it just cuts off a couple of words, its way too short, cutting off multiple sentences.
But again: the height does adjust. And that's the weird part. Its working - just not enough.
Any tips would be greatly appreciated!
You need to tell table view to dynamically scale the cells based on content:
tableView.rowHeight = UITableViewAutomaticDimension
Then, you can remove your implementation of heightForRowAtIndexPath: - cells should scale automatically. Remember to setup layout constraints correctly though. Without it dynamic sizing won't work.

How to set height of UITableView cell dynamically using Autolayout?

I had taken two view inside content view.
Height of "Post Container View" (Red background) is calculated dynamically as per height of label (All done using Autolayout).
Now I want that if height of "Post Container View" (Red background) will increase then height of cell view auto increase. I want to do this using autolayout.
I want to calculate height of UITableview cell using Autolayout. How to do it ?
Cell Height = Post Container View (Flexible as per label height)+ Image Container View Height (300 Fix)
I had seen this type of method, but dont know how to implement in my code ?
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell
{
sizingCell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(self.MyTableView.frame), CGRectGetHeight(sizingCell.bounds));
[sizingCell setNeedsLayout];
[sizingCell layoutIfNeeded];
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height + 1.0f; // Add 1.0f for the cell separator height
}
To calculate height of cell :
CGFloat height ; // take global variable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
height = cell.PostContainerView.frame.size.height ;
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
height = height + 300 //(300 is Fix height of Image Container View ) ;
return height ;
}
Do you want to calculate the height, or the tableView to calculate the height itself, considering your autolayout configuration ?
To do so, don't implement the delegate method heightForRowAtIndexPath but estimatedHeightForRowAtIndexPath instead (with whatever value you want for the moment).
The tableView will determine the height of the cell considering autolayout constraints applied on it.
Be warned that sometimes you need to call layoutIfNeeded on your cell, after you updated it. (for exemple in the cellForRowAtIndexPath)
Use this Extension class for string:
import UIKit
extension String {
func sizeOfString (font: UIFont, constrainedToWidth width: Double) -> CGSize {
return NSString(string: self).boundingRectWithSize(CGSize(width: width, height: DBL_MAX),
options: NSStringDrawingOptions.UsesLineFragmentOrigin,
attributes: [NSFontAttributeName: font],
context: nil).size
} }
In UITableView delegate calculate UILabel width:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let item = "hkxzghkfjkgjkfxkj jjhghdjajfjkshgjkhkkn jhkhhgdfgjkhsfjkdghhhsxzgnfshgfhk jhsfgfhjfhghj "
let widthOfLabel = Double(view.frame.size.width) - 30
let textHeight = item.sizeOfString(UIFont.systemFont(14), constrainedToWidth: widthOfLabel)
return (padding + textHeight.height)
}

UICollectionView inside a UITableViewCell -- dynamic height?

One of our application screens requires us to place a UICollectionView inside of a UITableViewCell. This UICollectionView will have a dynamic number of items, resulting in a height which must be calculated dynamically as well. However, I am running into problems trying to calculate the height of the embedded UICollectionView.
Our overarching UIViewController was created in Storyboards and does make use of auto layout. But, I don't know how to dynamically increase the height of the UITableViewCell based on the height of the UICollectionView.
Can anyone give some tips or advice on how to accomplish this?
The right answer is YES, you CAN do this.
I came across this problem some weeks ago. It is actually easier than you may think. Put your cells into NIBs (or storyboards) and pin them to let auto layout do all the work
Given the following structure:
TableView
TableViewCell
CollectionView
CollectionViewCell
CollectionViewCell
CollectionViewCell
[...variable number of cells or different cell sizes]
The solution is to tell auto layout to compute first the collectionViewCell sizes, then the collection view contentSize, and use it as the size of your cell. This is the UIView method that "does the magic":
-(void)systemLayoutSizeFittingSize:(CGSize)targetSize
withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority
verticalFittingPriority:(UILayoutPriority)verticalFittingPriority
You have to set here the size of the TableViewCell, which in your case is the CollectionView's contentSize.
CollectionViewCell
At the CollectionViewCell you have to tell the cell to layout each time you change the model (e.g.: you set a UILabel with a text, then the cell has to be layout again).
- (void)bindWithModel:(id)model {
// Do whatever you may need to bind with your data and
// tell the collection view cell's contentView to resize
[self.contentView setNeedsLayout];
}
// Other stuff here...
TableViewCell
The TableViewCell does the magic. It has an outlet to your collectionView, enables the auto layout for collectionView cells using estimatedItemSize of the UICollectionViewFlowLayout.
Then, the trick is to set your tableView cell's size at the systemLayoutSizeFittingSize... method. (NOTE: iOS8 or later)
NOTE: I tried to use the delegate cell's height method of the tableView -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath.but it's too late for the auto layout system to compute the CollectionView contentSize and sometimes you may find wrong resized cells.
#implementation TableCell
- (void)awakeFromNib {
[super awakeFromNib];
UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
// Configure the collectionView
flow.minimumInteritemSpacing = ...;
// This enables the magic of auto layout.
// Setting estimatedItemSize different to CGSizeZero
// on flow Layout enables auto layout for collectionView cells.
// https://developer.apple.com/videos/play/wwdc2014-226/
flow.estimatedItemSize = CGSizeMake(1, 1);
// Disable the scroll on your collection view
// to avoid running into multiple scroll issues.
[self.collectionView setScrollEnabled:NO];
}
- (void)bindWithModel:(id)model {
// Do your stuff here to configure the tableViewCell
// Tell the cell to redraw its contentView
[self.contentView layoutIfNeeded];
}
// THIS IS THE MOST IMPORTANT METHOD
//
// This method tells the auto layout
// You cannot calculate the collectionView content size in any other place,
// because you run into race condition issues.
// NOTE: Works for iOS 8 or later
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority verticalFittingPriority:(UILayoutPriority)verticalFittingPriority {
// With autolayout enabled on collection view's cells we need to force a collection view relayout with the shown size (width)
self.collectionView.frame = CGRectMake(0, 0, targetSize.width, MAXFLOAT);
[self.collectionView layoutIfNeeded];
// If the cell's size has to be exactly the content
// Size of the collection View, just return the
// collectionViewLayout's collectionViewContentSize.
return [self.collectionView.collectionViewLayout collectionViewContentSize];
}
// Other stuff here...
#end
TableViewController
Remember to enable the auto layout system for the tableView cells at your TableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
// Enable automatic row auto layout calculations
self.tableView.rowHeight = UITableViewAutomaticDimension;
// Set the estimatedRowHeight to a non-0 value to enable auto layout.
self.tableView.estimatedRowHeight = 10;
}
CREDIT: #rbarbera helped to sort this out
I think my solution is much simpler than the one proposed by #PabloRomeu.
Step 1. Create outlet from UICollectionView to UITableViewCell subclass, where UICollectionView is placed. Let, it's name will be collectionView
Step 2. Add in IB for UICollectionView height constraint and create outlet to UITableViewCell subclass too. Let, it's name will be collectionViewHeight.
Step 3. In tableView:cellForRowAtIndexPath: add code:
// deque a cell
cell.frame = tableView.bounds;
[cell layoutIfNeeded];
[cell.collectionView reloadData];
cell.collectionViewHeight.constant = cell.collectionView.collectionViewLayout.collectionViewContentSize.height;
Both table views and collection views are UIScrollView subclasses and thus don't like to be embedded inside another scroll view as they try to calculate content sizes, reuse cells, etc.
I recommend you to use only a collection view for all your purposes.
You can divide it in sections and "treat" some sections' layout as a table view and others as a collection view. After all there's nothing you can't achieve with a collection view that you can with a table view.
If you have a basic grid layout for your collection view "parts" you can also use regular table cells to handle them. Still if you don't need iOS 5 support you should better use collection views.
I read through all the answers. This seems to serve all cases.
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
collectionView.layoutIfNeeded()
collectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width , height: 1)
return collectionView.collectionViewLayout.collectionViewContentSize
}
Pablo Romeu's answer above (https://stackoverflow.com/a/33364092/2704206) helped me immensely with my issue. I had to do a few things differently, however, to get this working for my problem. First off, I didn't have to call layoutIfNeeded() as often. I only had to call it on the collectionView in the systemLayoutSizeFitting function.
Secondly, I had auto layout constraints on my collection view in the table view cell to give it some padding. So I had to subtract the leading and trailing margins from the targetSize.width when setting the collectionView.frame's width. I also had to add the top and bottom margins to the return value CGSize height.
To get these constraint constants, I had the option of either creating outlets to the constraints, hard-coding their constants, or looking them up by an identifier. I decided to go with the third option to make my custom table view cell class easily reusable. In the end, this was everything I needed to get it working:
class CollectionTableViewCell: UITableViewCell {
// MARK: -
// MARK: Properties
#IBOutlet weak var collectionView: UICollectionView! {
didSet {
collectionViewLayout?.estimatedItemSize = CGSize(width: 1, height: 1)
selectionStyle = .none
}
}
var collectionViewLayout: UICollectionViewFlowLayout? {
return collectionView.collectionViewLayout as? UICollectionViewFlowLayout
}
// MARK: -
// MARK: UIView functions
override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
collectionView.layoutIfNeeded()
let topConstraintConstant = contentView.constraint(byIdentifier: "topAnchor")?.constant ?? 0
let bottomConstraintConstant = contentView.constraint(byIdentifier: "bottomAnchor")?.constant ?? 0
let trailingConstraintConstant = contentView.constraint(byIdentifier: "trailingAnchor")?.constant ?? 0
let leadingConstraintConstant = contentView.constraint(byIdentifier: "leadingAnchor")?.constant ?? 0
collectionView.frame = CGRect(x: 0, y: 0, width: targetSize.width - trailingConstraintConstant - leadingConstraintConstant, height: 1)
let size = collectionView.collectionViewLayout.collectionViewContentSize
let newSize = CGSize(width: size.width, height: size.height + topConstraintConstant + bottomConstraintConstant)
return newSize
}
}
As a helper function to retrieve a constraint by identifier, I add the following extension:
extension UIView {
func constraint(byIdentifier identifier: String) -> NSLayoutConstraint? {
return constraints.first(where: { $0.identifier == identifier })
}
}
NOTE: You will need to set the identifier on these constraints in your storyboard, or wherever they are being created. Unless they have a 0 constant, then it doesn't matter. Also, as in Pablo's response, you will need to use UICollectionViewFlowLayout as the layout for your collection view. Finally, make sure you link the collectionView IBOutlet to your storyboard.
With the custom table view cell above, I can now subclass it in any other table view cell that needs a collection view and have it implement the UICollectionViewDelegateFlowLayout and UICollectionViewDataSource protocols. Hope this is helpful to someone else!
An alternative to Pablo Romeu's solution is to customise UICollectionView itself, rather than doing the work in table view cell.
The underlying problem is that by default a collection view has no intrinsic size and so cannot inform auto layout of the dimensions to use. You can remedy that by creating a custom subclass which does return a useful intrinsic size.
Create a subclass of UICollectionView and override the following methods
override func intrinsicContentSize() -> CGSize {
self.layoutIfNeeded()
var size = super.contentSize
if size.width == 0 || size.height == 0 {
// return a default size
size = CGSize(width: 600, height:44)
}
return size
}
override func reloadData() {
super.reloadData()
self.layoutIfNeeded()
self.invalidateIntrinsicContentSize()
}
(You should also override the related methods: reloadSections, reloadItemsAtIndexPaths in a similar way to reloadData())
Calling layoutIfNeeded forces the collection view to recalculate the content size which can then be used as the new intrinsic size.
Also, you need to explicitly handle changes to the view size (e.g. on device rotation) in the table view controller
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
Easiest approach I've came up with, so far, Credits to #igor answer above,
In your tableviewcell class just insert this
override func layoutSubviews() {
self.collectionViewOutlet.constant = self.postPoll.collectionViewLayout.collectionViewContentSize.height
}
and of course, change the collectionviewoutlet with your outlet in the cell's class
I was facing the same issue recently and I almost tried every solution in the answers, some of them worked and others didn't my main concern about #PabloRomeu approach is that if you have other contents in the cell (other than the collection view) you will have to calculate their heights and the heights of their constraints and return the result to get the auto layout right and I don't like to calculate things manually in my code. So here is the solution that worked fine for me without doing any manual calculations in my code.
in the cellForRow:atIndexPath of the table view I do the following:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//do dequeue stuff
//initialize the the collection view data source with the data
cell.frame = CGRect.zero
cell.layoutIfNeeded()
return cell
}
I think what happens here is that I force the tableview cell to adjust its height after the collection view height has been calculated. (after providing the collectionView date to the data source)
I would put a static method on the collection view class that will return a size based on the content it will have. Then use that method in the heightForRowAtIndexPath to return the proper size.
Also note that you can get some weird behavior when you embed these kinds of viewControllers. I did it once and had some weird memory issues I never worked out.
Maybe my variant will be useful; i've been deciding this task during last two hours. I don't pretend it's 100% correct or optimal, but my skill's very small yet and i'd like to hear comments from experts. Thank you.
One important note: this works for static table - it's specified by my current work.
So, all I use is viewWillLayoutSubviews of tableView. And a little bit more.
private var iconsCellHeight: CGFloat = 500
func updateTable(table: UITableView, withDuration duration: NSTimeInterval) {
UIView.animateWithDuration(duration, animations: { () -> Void in
table.beginUpdates()
table.endUpdates()
})
}
override func viewWillLayoutSubviews() {
if let iconsCell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1)) as? CategoryCardIconsCell {
let collectionViewContentHeight = iconsCell.iconsCollectionView.contentSize.height
if collectionViewContentHeight + 17 != iconsCellHeight {
iconsCellHeight = collectionViewContentHeight + 17
updateTable(tableView, withDuration: 0.2)
}
}
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch (indexPath.section, indexPath.row) {
case ...
case (1,0):
return iconsCellHeight
default:
return tableView.rowHeight
}
}
I know, that the collectionView is located in the first row of the second section;
Let the height of the row is 17 p. bigger, than its content height;
iconsCellHeight is a random number as the program starts (i know, that in the portrait form it has to be exactly 392, but it's not important). If the content of collectionView + 17 is not equal this number, so change its value. Next time in this situation the condition gives FALSE;
After all update the tableView. In my case its the combination of two operations (for nice updating of extending rows);
And of course, in the heightForRowAtIndexPath add one row to code.
I get idea from #Igor post and invest my time to this for my project with swift
Just past this in your
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//do dequeue stuff
cell.frame = tableView.bounds
cell.layoutIfNeeded()
cell.collectionView.reloadData()
cell.collectionView.heightAnchor.constraint(equalToConstant: cell.collectionView.collectionViewLayout.collectionViewContentSize.height)
cell.layoutIfNeeded()
return cell
}
Addition:
If you see your UICollectionView choppy when loading cells.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
//do dequeue stuff
cell.layer.shouldRasterize = true
cell.layer.rasterizationScale = UIScreen.main.scale
return cell
}
Pablo's solution did not work very well for me, I had strange visual effects (the collectionView not adjusting correctly).
What worked was to adjust the height constraint of the collectionView (as a NSLayoutConstraint) to the collectionView contentSize during layoutSubviews(). This is the method called when autolayout is applied to the cell.
// Constraint on the collectionView height in the storyboard. Priority set to 999.
#IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
// Method called by autolayout to layout the subviews (including the collectionView).
// This is triggered with 'layoutIfNeeded()', or by the viewController
// (happens between 'viewWillLayoutSubviews()' and 'viewDidLayoutSubviews()'.
override func layoutSubviews() {
collectionViewHeightConstraint.constant = collectionView.contentSize.height
super.layoutSubviews()
}
// Call `layoutIfNeeded()` when you update your UI from the model to trigger 'layoutSubviews()'
private func updateUI() {
layoutIfNeeded()
}
func configure(data: [Strings]) {
names = data
contentView.layoutIfNeeded()
collectionviewNames.reloadData()
}
Short and sweet. Consider the above method in your tableViewCell class. You would probably call it from func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell after dequeing your cell. Before calling reloadData on your collection view, in your tableCell, you need to tell the collection view to lay out its subviews, if layout updates are pending.
In your UITableViewDelegate:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ceil(itemCount/4.0f)*collectionViewCellHeight;
}
Substitute itemCount and CollectionViewCellHeight with the real values. If you have an array of arrays itemCount might be:
self.items[indexPath.row].count
Or whatever.
1.Create dummy cell.
2.Use collectionViewContentSize method on UICollectionViewLayout of UICollectionView using current data.
You can calculate the height of the collection based on its properties like itemSize, sectionInset, minimumLineSpacing, minimumInteritemSpacing, if your collectionViewCell has the border of a rule.

Resources