I am building an app that lets the user create a "Story" which consists of a title and a text.
I am implementing a tableView that shows all created stories. So far everything works. But here is my issue:
When the user enters a title or text that is longer that what tableViewCell would be able to display, that cell doesn't show up at all.
Others with shorter names still do though.
I am using the cell style "subtitle".
How does one go about limiting the amount of text showing in the cell and what causes this bug? Because even if I find a way to fix it, there will probably still be a problem with text running off the screen.
Here is the code in my UITableViewController class:
class StoryTableViewController: UITableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return savedStories.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)
cell.textLabel?.text = savedStories[indexPath.row].title
cell.detailTextLabel?.text = savedStories[indexPath.row].text
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
Here is a screenshot of the UI from the interface builder:
You need to create your custom UITableViewCell. And you can use available dynamic resizable cells for adjusting cell automatically to text length.
IB steps:
Make a UILabel on cell. Don't give any height constraint to it. just pin it up from all sides and do the followings :
label.numberOfLines = 0
In viewDidLoad:
self.tableView.estimatedRowHeight = 88.0 //Any estimated Height
self.tableView.rowHeight = UITableViewAutomaticDimension
Don't write heightForRow: method but if you want to use it because of several cells existing there , you can return UITableViewAutomaticDimension for that particular cell height.
Try this one out
You have to implement these two delegates, don't forget to bind tableView delegate and datasource with VC and set you label description property numberOfLines = 0 from storyboard.
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60; // height of default cell
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension; // It takes automatic height of cell
}
Now in storyboard do this below
Your view hierarchy should be like this
check only ViewLabelContatainer
Add a view and put all labels into it.
Label Container contraints
Label Title constraint
Label Description constraint
output
For that you need to use variable height TableViewCell
override func viewDidLoad()
{
super.viewDidLoad()
self.tableView.estimatedRowHeight = 200 // give maximum height you required
self.tableView.rowHeight = UITableViewAutomaticDimension
}
then add this delegate methode in your view controller
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
I have a UITableView inside a regular ViewController and I'd like to adjust the tableview's entire height.
I created the tableview through storyboard, and I'm loading data from an API in my ViewDidLoad. I set up the cells in the typical tableview method.
override func viewDidLoad() {
self.loadPerformers()
setupTableView(performersTableView)
}
func setupTableView(tableView: UITableView) {
tableView.dataSource = self
tableView.delegate = self
tableView.estimatedRowHeight = 90
tableView.rowHeight = UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if tableView == self.performersTableView {
let performerCell = tableView.dequeueReusableCellWithIdentifier("PerformerCell", forIndexPath: indexPath) as! PerformersTableViewCell
let performer = self.performersArray[indexPath.row]
performerCell.performerPic.image = getPerformerImage(performer.english_name)
performerCell.performerName.text = performer.english_name + " " + performer.japanese_name
cell = performerCell
}
return cell!
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count:Int?
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
tableView.separatorColor = UIColor.clearColor()
tableView.allowsSelection = false
if tableView == self.performersTableView {
count = self.performersArray.count
}
return count!
}
The problem I'm having is the tableview stays the same height after loading the data. Below I'm loading four items, but it's only showing 2. The tableview height doesn't change.
I've tried a few things, among them is changing the height of the tableview frame in the viewWillAppear:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
let newHeight = self.performersTableView.rowHeight * CGFloat(self.numberOfPerformers)
self.performersTableView.frame = CGRectMake(performersTableView.frame.origin.x, performersTableView.frame.origin.y,
performersTableView.frame.size.width, newHeight)
}
This does nothing though, so I'm misunderstanding something in the rendering process. How do I redisplay the table with my desired height?
I have 2 solutions for your problem:
If you want to set height of Tableview based on Tableview Cells.
so first of when you get data from server Reload your Tableview and after reloading tableview you will get content height of Filled tableview using this code:
self.performersTableView.contentSize.height
then set this height as your tableview frame.
In design, set fix height of tableview and set reference of constraint and after reloading tableview:
self.performersTableViewHeightReference.constant = self.performersTableView.contentSize.height
I hope this helps You.
[self.theTableView setNeedsLayout];
[self.theTableView layoutIfNeeded];
Add this code in your setupTableView and numberOfRowsInSection is data count. And if you want to expend cell also according to content write code in cellForRowAtIndexpath: cell.detailTextLabel.numberOfLines = 0;
Text data of variable length are being injected into tableview cell labels. In order for each cell height to be properly sized, I have implemented in viewDidLoad():
self.tableView.estimatedRowHeight = 88.0
self.tableView.rowHeight = UITableViewAutomaticDimension
This estimates the height to be 88.0 pixels and should resize the height automatically if larger. It works perfectly for cells that have yet to be scrolled to (as UITableViewAutomaticDimention is called upon scrolling to the cell), but not for the cells that are initially rendered onscreen upon loading the table with data.
I have tried reloading the data (as suggested in many other resources):
self.tableView.reloadData()
in both viewDidAppear() and viewWillAppear() and it did not help. I am lost.. does anyone know how to render the dynamic height for the cells loaded initially on screen?
Try This:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
EDIT
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 4
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 4.2
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Define above Both Methods.
It solves the problem.
PS: Top and bottom constraints is required for this to work.
Here is example
Use this:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 300
and don't use: heightForRowAtIndexPath delegate function
Also, in the storyboard don't set the height of the label that contains a large amount of data. Give it top, bottom, leading, trailing constraints.
SWIFT 3
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 160
AND!!!
In storyBoard: You HAVE TO set TOP & BOTTOM constraints for your Label.
Nothing else.
This strange bug was solved through Interface Builder parameters as the other answers did not resolve the issue.
All I did was make the default label size larger than the content potentially could be and have it reflected in the estimatedRowHeight height too. Previously, I set the default row height in Interface Builder to 88px and reflected it like so in my controller viewDidLoad():
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 88.0
But that didn't work. So I realized that content wouldn't ever become larger than maybe 100px, so I set the default cell height to 108px (larger than the potential content) and reflected it like so in the controller viewDidLoad():
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 108.0
This actually allowed the code to shrink down the initial labels to the correct size. In other words, it never expanded out to a larger size, but could always shrink down... Also, no additional self.tableView.reloadData() was needed in viewWillAppear().
I know this does not cover highly variable content sizes, but this worked in my situation where the content had a maximum possible character count.
Not sure if this is a bug in Swift or Interface Builder but it works like a charm. Give it a try!
Set automatic dimension for row height & estimated row height and ensure following steps:
#IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Set automatic dimensions for row height
// Swift 4.2 onwards
table.rowHeight = UITableView.automaticDimension
table.estimatedRowHeight = UITableView.automaticDimension
// Swift 4.1 and below
table.rowHeight = UITableViewAutomaticDimension
table.estimatedRowHeight = UITableViewAutomaticDimension
}
// UITableViewAutomaticDimension calculates height of label contents/text
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// Swift 4.2 onwards
return UITableView.automaticDimension
// Swift 4.1 and below
return UITableViewAutomaticDimension
}
For Example: if you have a label in your UITableviewCell then,
Set number of lines = 0 (& line break mode = truncate tail)
Set all constraints (top, bottom, right left) with respect to its superview/ cell container.
Optional: Set minimum height for label, if you want minimum vertical area covered by label, even if there is no data.
Here is sample label with dynamic height constraints.
For Swift 3 you can use the following:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Dynamic sizing cell of UITableView required 2 things
Setting the the right constraint of your view inside the table view cell (mostly it includes giving your view proper top , bottom and traling constraints)
Calling these properties of TableView in viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140
This is a wonderfull tutorial on self-sizing (dynamic table view cells) written in swift 3 .
In my case - In storyboard i had a two labels as in image below,
both labels was having desired width values been set before i made it equal. once you unselect, it will change to automatic, and as usual having below things should work like charm.
1.rowHeight = UITableView.automaticDimension, and
2.estimatedRowHeight = 100(In my case).
3.make sure label number of lines is zero.
In addition to what others have said,
SET YOUR LABEL'S CONSTRAINTS RELATIVE TO THE SUPERVIEW!
So instead of placing your label's constraints relative to other things around it, constrain it to the table view cell's content view.
Then, make sure your label's height is set to more than or equal 0, and the number of lines is set to 0.
Then in ViewDidLoad add:
tableView.estimatedRowHeight = 695
tableView.rowHeight = UITableViewAutomaticDimension
To make autoresizing of UITableViewCell to work make sure you are doing these changes :
In Storyboard your UITableView should only contain Dynamic Prototype Cells (It shouldn't use static
cells) otherwise autoresizing won't work.
In Storyboard your UITableViewCell's
UILabel has configured for all 4 constraints that is top, bottom,
leading and trailing constraints.
In Storyboard your UITableViewCell's
UILabel's number of lines should be 0
In your UIViewController's
viewDidLoad function set below UITableView Properties :
self.tableView.estimatedRowHeight = <minimum cell height>
self.tableView.rowHeight = UITableViewAutomaticDimension
For Swift i checked this answer in iOS 9.0 and iOS 11 also (Xcode 9.3)
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Here you need to add top, bottom, right and left constraints
For Swift 4.2
#IBOutlet weak var tableVw: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Set self as tableView delegate
tableVw.delegate = self
tableVw.rowHeight = UITableView.automaticDimension
tableVw.estimatedRowHeight = UITableView.automaticDimension
}
// UITableViewDelegate Method
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
Happy Coding :)
This is simple when doing 2 things:
setting the automatic height
tableView.rowHeight = UITableView.automaticDimension
creating all TableViewCells with FULL constraints from top to bottom. The last element MUST define some bottom spacing to end the cell.
So the layout engine can compute the cell heigth and apply the value correctly.
Unfortunately, I am not sure what I was missing. The above methods don't work for me to get the xib cell's height or let the layoutifneeded()or UITableView.automaticDimension to do the height calculation. I've been searching and trying for 3 to 4 nights but could not find an answer.
Some answers here or on another post did give me hints for the workaround though. It's a stupid method but it works. Just add all your cells into an Array. And then set the outlet of each of your height constraint in the xib storyboard. Finally, add them up in the heightForRowAt method. It's just straight forward if you are not familiar with the those APIs.
Swift 4.2
CustomCell.Swift
#IBOutlet weak var textViewOneHeight: NSLayoutConstraint!
#IBOutlet weak var textViewTwoHeight: NSLayoutConstraint!
#IBOutlet weak var textViewThreeHeight: NSLayoutConstraint!
#IBOutlet weak var textViewFourHeight: NSLayoutConstraint!
#IBOutlet weak var textViewFiveHeight: NSLayoutConstraint!
MyTableViewVC.Swift
.
.
var myCustomCells:[CustomCell] = []
.
.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("CustomCell", owner: self, options: nil)?.first as! CustomCell
.
.
myCustomCells.append(cell)
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let totalHeight = myCustomCells[indexPath.row].textViewOneHeight.constant + myCustomCells[indexPath.row].textViewTwoHeight.constant + myCustomCells[indexPath.row].textViewThreeHeight.constant + myCustomCells[indexPath.row].textViewFourHeight.constant + myCustomCells[indexPath.row].textViewFiveHeight.constant
return totalHeight + 40 //some magic number
}
I use these
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
Try
override func viewWillAppear(animated: Bool) {
self.tableView.layoutSubviews()
}
I had the same problem and it works for me.
You should just set all constraints for TOP, BOTTOM and HEIGHT for each object on cell view/views and remove exists middle Y position if have. Because where you didn't this, puts artifacts on another views.
For objective c this is one of my nice solution. it's worked for me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text = [_nameArray objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
We need to apply these 2 changes.
1)cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
2)return UITableViewAutomaticDimension;
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.estimatedRowHeight = 88.0
And don't forget to add botton constraints for label
I was just inspired by your solution and tried another way.
Please try to add tableView.reloadData() to viewDidAppear().
This works for me.
I think the things behind scrolling is "the same" as reloadData. When you scroll the screen, it's like calling reloadData() when viewDidAppear .
If this works, plz reply this answer so I could be sure of this solution.
I had also got this issue initially, I had resolved my issue from this code
try avoiding the use of self.tableView.reloadData() instead of this code for dynamic height
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationFade];
When using a static UITableView, I set all the values in the UILabels and then call tableView.reloadData().
What worked for me was creating a height constraint on my custom cell that I set at runtime (I've got an expand/collapse button in each cell).
Then in heightForRowAt in the parent, I had to do a combination of suggested answers:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if let cell = tableView.cellForRow(at: indexPath) as? GroupTableViewCell {
return cell.heightConstraint.constant
}
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 88.0
}
I use the already calculated height constraint constant where it's available and UITableView.automaticDimension otherwise. This was the only way to get the correct height and maintain the correct cell state when the cell gets recycled.
I hear it's considered bad practice to reference the cell itself inside heightForRowAt, but I don't see another way of doing it with custom cell objects with dynamic heights whilst keeping all constraints satisfied.
self.Itemtableview.estimatedRowHeight = 0;
self.Itemtableview.estimatedSectionHeaderHeight = 0;
self.Itemtableview.estimatedSectionFooterHeight = 0;
[ self.Itemtableview reloadData];
self.Itemtableview.frame = CGRectMake( self.Itemtableview.frame.origin.x, self.Itemtableview.frame.origin.y, self.Itemtableview.frame.size.width,self.Itemtableview.contentSize.height + self.Itemtableview.contentInset.bottom + self.Itemtableview.contentInset.top);
Set proper constraint and update delegate methods as:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
This will resolve dynamic cell height issue. IF not you need to check constraints.
Swift 5 Enjoy
tablev.rowHeight = 100
tablev.estimatedRowHeight = UITableView.automaticDimension
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tablev.dequeueReusableCell(withIdentifier: "ConferenceRoomsCell") as! ConferenceRoomsCell
cell.lblRoomName.numberOfLines = 0
cell.lblRoomName.lineBreakMode = .byWordWrapping
cell.lblRoomName.text = arrNameOfRooms[indexPath.row]
cell.lblRoomName.sizeToFit()
return cell
}
I have a tableview set up to load from an array. I want the first item to show in the table as the "user's profile" so the height is set differently from the other cells like so:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
//some conditional to see which cell we use
//if it's the first cell make it the profile cell - basic cell
if (indexPath.row==0) {
//set the height
self.tableView.rowHeight = 230.0
//call the profile cell
return basicCellAtIndexPath(indexPath)
} else //do they have friends?
{
//set the height
self.tableView.rowHeight = 70.0
//get the friend var
let item: String = TableDataFriends[indexPath.row]
if(item=="nofriends") {
//no friends. Tell them to invite some
return inviteCellAtIndexPath(indexPath)
} else {
//got friends. Show them with the friend cell with image
return imageCellAtIndexPath(indexPath)
}
}
}
It works fine when the page loads initially, however when I navigate from the table page to any other scene and then back, all cell heights are 70 and the "profile cell" is now merged layered behind the other cells. I've tried a few different things to ensure that the height is set up properly. The below two functions I tried did nothing.
//test for table height
override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath==1) {
return 230.0
} else {
return 70.0
}
}
//test for table height
func configureTableView() {
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 230.0
}
How do I make sure the first cell's height stays at 230 and the others remain at 70 between page navigation? Thanks!
I have implemented variable row heights by overriding the UITableViewDelegate method tableView(_:heightForRowAtIndexPath:). You were on the right track with
tableView(_:estimatedHeightForRowAtIndexPath:).
A note about what you attempted in your tableView(_:cellForRowAtIndexPath:) override: The rowHeight property applies to all rows in the tableView, so while you think you're changing it for one row, you're actually changing it for all of them.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if(indexPath==0) {
return 230.0
}
else {
return UITableViewAutomaticDimension
}
}
You need not specify anything related to height in cellForRowAtIndexPath .
All I had to do is add the following and remove the rowHeight.
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
let ix = indexPath.row
if ix == 0 {
return 230.0
}
else {
return 70.0
}
}
I have not added any prototype cells but it should work according to the latest iOS 8 tableView. Here is my code
class ViewController: UIViewController,UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
var tabledata = ["hn, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjks" , "hi i am ishan, helooo dnsjakdnksandksajndksandkasjnkc sacjkasc jkas kjdjknasjkdnsaklmdlksamxklsamxlksamdklsandk cnsdjdnsklnjnfkdnflasfnlfnkdsnfjkdnfjkdsnjkd njsdkadnaksjdnjkasndsakdnkasdnsalkdn cjkndskasdnsakndksandjksandksajndkj ndsjkadnksalndls;adnklsa;mdas,mcjksanckasdjnklscaskncjkssndjkasndjksandkjasndkjasndkjasndkjasndjka ", "a" ]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tabledata.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.textLabel.text = self.tabledata[indexPath.row]
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
tableView.reloadData()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I don't see any changes when I remove/add these lines
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
For automatic cell sizing to work, you must add layout constraints to the cell subviews that fully describe the vertical size of the views. Without these constraints, your cell's have no way of knowing how big they actually need to be. This is most easily done in a storyboard.
estimatedRowHeight is just a hint to the table view that increases the table load time by deferring geometric calculations for the cells to scroll time (autolayout can be expensive to calculate). Autolayout is still required to tell the table view what size each cell should be.
Also worth noting is that you're not reusing cells in your table view. In your tableView(_:cellForRowAtIndexPath:) method, you should be dequeuing cells instead of creating a new one every time:
func tableView(tableView: UITableView, cellForRowAtAindexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
// configure cell...
return cell
}
1 :Add in ViewDidLoad
tableView.estimatedRowHeight = 140 // prototype cell height
tableView.rowHeight = UITableViewAutomaticDimension
2:reload your tableview in viewDidAppear
Important NOTE:to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view.
It will helps to give Table View Row Height Dynamically
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *dictItinararay=[arrCodes objectAtIndex:indexPath.row];
CGSize sizeOfName = [[dictItinararay valueForKey:#"Name"] sizeWithFont:[UIFont systemFontOfSize:16] constrainedToSize:CGSizeMake(180, 1000) lineBreakMode:NSLineBreakByWordWrapping];
if(sizeOfName.height>45) {
return sizeOfName.height+10;
}
else {
return 45;
}
}
you can simple add line to set cell height
cell.textLabel.numberOfLines = 20