Setting Row Height At cellForRowAtIndexPath For Different Row Heights - ios

Tableview has three types of custom cells with dynamic height. I want to set the row height using like tableView.rowHeight = 100 in cellForRowAtIndex() method.
Is this appropriate?

1.Define auto layout constraints for your prototype cell
2.Specify the estimatedRowHeight of your table view
3.Set the rowHeight of your table view to UITableViewAutomaticDimension
Do these code in viewDidLoad
tableView.estimatedRowHeight = 68.0(actual height of your prototype cell)
tableView.rowHeight = UITableViewAutomaticDimens

No, that is not how you need to do it. Never make any attempt to set any table attributes inside the cellForRowAtIndexPath method. Just create and return a cell.
If your cells have varying heights, simply implement the heightForRowAtIndexPath method and return the appropriate height for a given indexPath.
If all rows have the same height, then set the table view's rowHeight property in viewDidLoad or other appropriate place.

Related

How do i set UICollectionView height automatically inside UITableView

I want to make my UICollectionView height automatically because the data inside the collection view cell is dynamic.
If i'm delegate the table view as tableView.rowHeight = UITableViewAutomaticDimension, it'll not call the cellForItemAtIndex. But if i delegate the table view height, it'll call func cellForItemAtIndex, but the size'll be static.
How to make that height automatically?

Auto set height of table view based on UICollectionView inside it

I have a UITableView with cells that have inside a UICollectionView.
Now all works great except of UICollectionViewCell scrolling i want that the height of table view cell follow the Collection view cell content.
In simple word i want scrolling in table view but not inside single cell i need to display all contents of UIcollectionViewCell without scrolling inside it.
Thanks
You have to calculate the collectionviews' heights and set a height constraint on each of them. then add top and bottom constraints from the collectionviews to their superviews (the tableviewcells / their contentviews) and let autolayout do its work.
don't forget
tableView.estimatedRowHeight = 44 // or something similar
tableView.rowHeight = UITableViewAutomaticDimension
in the controller's viewDidLoad to make the automatic row height calculation work.

How to calculate tableView row height and pass value to heightForRowAtIndexPath

I've a tableView with custom cells.
Each cell has different interface, elements. All they have different sizes.
Each cell has different type of elements and their count.
All these elements are created dynamically, so I'm creating them, making their frames and adding as subviews.
So the problem that heightForRowAtIndexPath executes before cellForRowAtIndexPath where I'm creating the row and constructing its interface and I don't know how to pass calculated height to heightForRowAtIndexPath
How can I count row height before and pass its value to heightForRowIndexPath for correct drawing of my tableView?
If your cell view is really very complex and every component's height are depending on data source. You can try to create the view in heightForRowIndexPath method and then cache the created view to a dictionary in your view controller and use it directly in cellForRowAtIndexPath. In this way you only need to create the view once when user scrolling the table. If the datasource is not changing very frequently, you can reuse the cached view in heightForRowIndexPath as well.
And if the tableview has a lot of rows, you should return an approximate value for height in estimatedHeightForRowAtIndexPath to speed up the loading process of the view controller. Otherwise during loading tableview, it will try to calculate all row's height which may requires a lot of time.
But I really don't think your cell would be so complex. If only some UITextLabels that depends on datasource for the height, you can simply only calculate the height for the label, then add it to other components' height which is fixed.
You really should consider subclassing UITableViewCell and adding your custom logic inside your subclass.
Starting from there, you'll have such options:
Create static method in your cell that will receive all data necessary to draw your cell (e.g heithtForCellWithFirstString:secondString:accessoryImage etc) and calculate height using string size computation methods. Use this method inside heightForRowAtIndexPath.
Use autolayout for laying out subviews of your cell and then set table view's row height property to UITableViewAutomaticDimension. This way you won't need heightForRow delegate method at all. There are plenty of tutorials on this, for example: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift
Try to create a subclass of UITableViewCell.
All elements created by you put inside UIView, which is put in a cell.
Create three NSLayoutConstraint, one from UIView.top to top of the cell, the second from UIView.bottom to the bottom of the cell and the third - the height of the UIView.
In the viewDidLoad method of tableViewController set row height as the UITableViewAutomaticDimension
:
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.rowHeight = UITableViewAutomaticDimension
}
In a subclass of UITableViewCell create a method that will calculate the height of the cell and change NSLayoutConstraint, which sets the height of the cell. For example:
:
func adjustHeightOfInnerView() {
let height = 100
self.myInnerCellViewHeightConstraint.constant = height
self.contentView.setNeedsUpdateConstraints()
}
In the method cellForRowAtIndexPath call the adjustHeightOfTableview:
:
func tableView (tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! myTableViewCellSubclass
cell.adjustHeightOfTableview()
return cell
}

Create a view table with static cells of equal height to fill screen

How do I create a table view with static cells, where the row heights are all equal to make the set of static cells fill the table view?
The row height needs to be adjusted dynamically to account for the different iOS screen sizes.
For example I want a table view with 7 static cells of equal heights that fill the rest of the screen space except the navigation bar.
I've tired using the following with no luck:
self.tableView.rowHeight = self.tableView.frame.height/6;
and
override func tableView(tableView: UITableView,
heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return self.tableView.frame.height/6;
}
and the autoLayout feature in storyboards is grayed out when I try to use it with any part of the table view.
Appreciate any help thanks!
Define auto layout constraints for your prototype cell.
Specify the estimatedRowHeight of your table view.
Set the rowHeight of your table view to UITableViewAutomaticDimension.
If we express the last two points in code, it looks like this.
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension

What is the different between set a UITableView cell height with setRowHeight function and tableView: heightForRowAtIndexPath: function?

Just like the title, what's the different between this two method?
I find that if I set its height with tableView: heightForRowAtIndexPath:, and then I want to get the value of height, I will get a right value by this:
CGFloat height = cell.frame.size.height;
but a wrong value by this, the result is 44:
CGFloat height = tableview.rowHeight;
But if I set it in the other way -- setRowHeight:, I can get right value in both the two way.
So what's the different between them?
rowHeight is a property on UITableView and is used for setting the table row height when the delegate does not implement tableView:heightForRowAtIndexPath: method.
From Apple docs,
You may set the row height for cells if the delegate doesn't implement the tableView:heightForRowAtIndexPath: method. If you do not explicitly set the row height, UITableView sets it to a standard value.
When row height is returned from delegate method tableView:heightForRowAtIndexPath: the value of property rowHeight is set to default (IMO, 44 points).
Apple recommends rowHeight in special use cases,
There are performance implications to using tableView:heightForRowAtIndexPath: instead of rowHeight. Every time a table view is displayed, it calls tableView:heightForRowAtIndexPath: on the delegate for each of its rows, which can result in a significant performance problem with table views having a large number of rows (approximately 1000 or more).
In my experience tableView:heightForRowAtIndexPath: is used when cell height is dynamic. For example, you have to display multiline text of varying height in each cell. In such cases, calculate and return the dynamic height of the cell using this delegate method.
Hope that helps!

Resources