tableView not setting automatic row height - ios

In my project, I have a static tableView with 3 sections. The cell in the second section holds a label that is filled dynamically and therefore has a dynamic height. The cell should adjust its height to the label's height. Here's what I tried, without success:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 44
} else if indexPath.section == 1 {
return UITableViewAutomaticDimension
} else if indexPath.section == 2 {
return 80
} else {
return 50
}
}
The heights of all sections are set properly except the automatic dimensions. Any help?

set this line in viewDidLoad()
tableView.rowHeight = UITableViewAutomaticDimension
then write this table view method
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
also make sure you have used the auto layout properly.
and you have set the number of lines for lable = 0

You also need to provide an estimated row height. You can do that by using the estimatedRowHeight property of your UITableView, or implementing the corresponding delegate method:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return XXXXX // Provide your estimation here, or pass UITableViewAutomaticDimension (not the best for performances)
}
Reference: https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html

Related

Change static tableview cell height dynamically

I have a tableview. I created it from storyboard it is a static tableview.(I am using it for profile settings).
But i have to change height of the first row of this table view dynamically and i don't want to change other cell heights. They should use their storyboard height values. How can i do this?
I tried following code:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return (tableView.cellForRow(at: indexPath)?.bounds.height)!
}
But this is setting 100 for all cells.
My tableview:
Use the following override function..
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
}
return UITableView.automaticDimension
}
But you have to make sure that every item in the static tableview have (top/bottom/left/right) constraints. it only works when it have all these constraints on it.

Xcode Table View Cells won't resize height

I am trying to have a UITableView with different sized prototype cells.
I set the row height in my Table View to 243. The first and second cells' individual row height are set to 243 and 465, respectively. When I run my app, both cells appear at 243 height.
Here is what I have tried:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//does not change cell height
And:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 25
}
//Method does not override any method from its superclass
How can I configure my project to successfully resize the second cell?
The signature of the delegate method has changed in Swift 3. It needs to be:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 1 {
return 465.0
} else {
return 243.0
}
}
With the wrong signature the table view doesn't know you've implemented the method so it never gets called.

Static UITableView, make a single cell with dynamic height in Swift

I have a static UITableView which has 12 rows and for 11 rows I know what the height needs to be.
I have a UILabel which has dynamic text which sits inside the 12 row, how do i go about making just that one cell to have a dynamic height based on the text in the UILabel.
I have tried the below code inside viewDidLoad, but it didn't work. The rows remained the same height. I have also set the lines = 0 for the UILabel
tableView.estimatedRowHeight = 100.0
tableView.rowHeight = UITableViewAutomaticDimension
Have you tried this?
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if (indexPath.row < 11) {
// Everything starts at 0, so this covers 0-10
// Whatever your cell height is
return <#fixedCellHeight#>
} else {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
}
}
Adding to #Adrian answer , if you are using static Cells , changing one cell to dynamic height , and others as they are you can edit it to this .
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 11 {
// This is your 12th cell (indexPath.row 11), as we start counting at 0
return UITableViewAutomaticDimension
} else {
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
}
Try this:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
You can create 2 cell prototype cells for the table view. You give them 2 different id.
And then in your code you override this fonction
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = UITableViewCell()
if indexPath.row < 12 {
cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
} else {
cell = tableView.dequeueReusableCellWithIdentifier("cell12", forIndexPath: indexPath)
}
return cell
}
You Can create the dynamic cell with Text height then you can set the static and dynamic cell both in on table view
Here is link to dynamic cell with text How to change cell height dynamically in UITableView static cell
More elegant might be to just implement as suggested:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Or in Objective C:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;
}
But rather than the row specific logic, add constraints to your static cell's content in the Storyboard to keep the row heights constant. That way if you move rows or change content you don't need to change any code.

UITableView Row Heigh Changing Between Scenes (swift)

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
}
}

Wrap Text in UITableView Custom Cell When It Reaches End of Screen (Swift)

I have a table that loads data from a database but the problem is if the text being loaded is too long, it goes off the screen. I'm trying to find a way for the text to go onto the next line and have the cell resize automatically to fit this change. Does anyone know how this is done? The cell has three labels but one of them is allowed to be multi-lined.
EDIT:
I got it to work using auto constraints but how can I resize the table cell so that the actual items fit inside the cell and do not go over the cell boundary?
Set label number of "Lines" to ZERO. And set "Line Breaks" to "Word Wrap"
Adding these two methods along with above code fixed the problem with custom font
tamilRow.textLabel?.numberOfLines=0
tamilRow.textLabel?.lineBreakMode = NSLineBreakMode.ByWordWrapping
func tableView(tableView: UITableView, heightForRowAtIndexPath
indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
swift 5
let unselectedCellHeight: CGFloat = 130.0
func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if selectedCellIndexPath == indexPath {
//return selectedCellHeight
return UITableView.automaticDimension
}
return unselectedCellHeight
}

Resources