I have a UITableView and have my own custom cell containing a UILabel inside the cell.
I want to increase the tableView row size according to the text in the label.
I am using AutoLayout and my label have 20 padding from left and 8 padding from right,top and bottom.
I am stuck here because the label context is some time small and sometimes very large and i want to increase the size of that row when the text is increased.
Set the UILabel.numberOfLines to 0 and add this to your UITableViewDelegate
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Related
I am trying to make a Chat app. I have a tableView and prototype cell in which I have UILabel. But I am stuck with label size(HxW). Please take a look in the image below:
The first label is taking extra width and height is less. I want that label should take width according to its content but till the button.
These are the contraints:
For the height I am using:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
I have a tableView, and within each UITableViewCell (call this parentCell), I have another tableView which has cells (call this childCell). I am using UITableViewAutomaticDimension to dynamically resize both of the cells height. I am successfully able to resize the height of the childCell this way, however, I am not able to apply UITableViewAutomaticDimension to the parentCell, since there when heightForRow is called, the childCell's height have yet to be estimated.
Is it possible to use UITableViewAutomaticDimension to estimate the parent cells height, which will be the height of all the childCell's height added together? Or am I going to have to do this manually?
//in ViewDidLoad
parentTableView.rowHeight = UITableViewAutomaticDimension
//in ParentCell's tableView's cell for row
cell.childTableView.rowHeight = UITableViewAutomaticDimension
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
The only way to do this was to use sections instead of placing a tableview within a cell.
In Autolayout, how to give percentage to UITableViewCell height base on the height of UITableView? Then All UITableViewCell can show out in One View without scroll.
First make a property for numberOfRows to be returned in tableView. Let's say rowCount is the property name. Then
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return tableView.frame.size.height / rowCount
}
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
}
I'm trying to retrieve images and then display them in a cell. The width of every image should be the same, but the height can vary - and therefore, I need the cell size to adjust automatically.
I do this to retrieve the image and scale it:
cell.thumbnail.sd_setImageWithURL(NSURL(string: imageURL), placeholderImage: blankImage)
cell.thumbnail.contentMode = UIViewContentMode.ScaleToFill
and this to get the cell size to change dynamically:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}
But I can't get it to work!
The problems are:
1) The image appears super stretched out in height
2) I have to scroll past the image and then back to it just to see the ratio change
3) The scrolling is super glitchy (it's NOT because of the image load)
4) I have a segue from that leads from the cell to another view controller. Once I click on the cell, the entire table glitches for a second. When I got back to it, all the images are resized in a completely different way.
I set the constraints correctly so I know it's not a problem with that.
Try calling this as well:
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
When initializing table view do the following:
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 500 //Put just any approximate average height for cell. This will just be used to show scroll bar offset.
Remove the method:
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension;
}