I have JSON object where one key is interface_id and I want to set Cell height accordingly, When I try to set height 0, after that tableView stuck while scrolling.
public func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat{
if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
return 0
}else{
return UITableViewAutomaticDimension
}
}
Basically, to use self sizing cells, for the UITableView you will set an estimated row height, and also set the rowHeight to a value of UITableViewAutomaticDimension. so changing the Cell height for row your code should be look like that.
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 44.0
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (dataArray[indexPath.row] as AnyObject).value(forKey: "interface_id") as! Int == 1{
return 0
}else{
return UITableViewAutomaticDimension
}
}
Related
I have a UITableViewController with a custom UITableViewCell. Each cell has 2 labels. When the cell is selected it expands to a fixed value, I do it via tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat . I also have set the rowHeight = UITableViewAutomaticDimension , as some cells have to display multiple lines of text.
What I want to achieve is when a cell needs to be expanded I want to add 50 points to its current height. So here's the question, how can I get current height of the cell, when the rowHeight = UITableViewAutomaticDimension is set?
Here's my code for the fixed height for the selected state:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if selectedIndexPath == indexPath {
return selectedHeight
}else{
return tableView.estimatedRowHeight
}
}
EDIT: I also need to change it after that, by adding some variable to it.
Building on HamzaLH's answer you might do something like this...
import UIKit
class TableViewController: UITableViewController {
var selectedRow: Int = 999 {
didSet {
tableView.beginUpdates()
tableView.endUpdates()
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == selectedRow { //assign the selected row when touched
let thisCell = tableView.cellForRow(at: indexPath)
if let thisHeight = thisCell?.bounds.height {
return thisHeight + 50
}
}
return 60 //return a default value in case the cell height is not available
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedRow = indexPath.row
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.detailTextLabel?.text = "test"
return cell
}
}
I'm animating the expansion of the height with a didSet when a the selectedRow is altered.
Also, don't forget you may still need to connect your dataSource and delegate by dragging the outlets in Interface Builder to your View Controller in the storyboard. After that you still need to add this to ViewDidLoad in your ViewController's swift file.
tableView.delegate = self
tableView.dataSource = self
I also have an Outlet declared for the tableView like below, and connected in Interface Builder storyboard.
#IBOutlet weak var tableView: UITableView!
You need to get the cell using cellForRow and then get the height of the cell.
let cell = tableView.cellForRow(at: indexPath)
let height = cell.bounds.height
I have cells with dynamic height inside an UITableView.
In interface builder, everything looks good.
These are my constraints:
But on the device, there is added empty space at the top and bottom of some messages. There would be no problem if the label's greater than or equal constraint would be an equality constraint, but the bubbles have the width of the screen.
This is my cell configuration:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row % 2 == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "ReceivedMessage") as! MessageTableViewCell
cell.messageLabel.text = arr[indexPath.row]
cell.setNeedsLayout()
cell.setNeedsDisplay()
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "SentMessage") as! MessageTableViewCell
cell.messageLabel.text = arr[indexPath.row]
cell.setNeedsLayout()
cell.setNeedsDisplay()
return cell
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 70
}
Any idea why this is happening? Thanks.
I have a UILabel in UITableViewCell.I have given it's constraints as Top,Bottom,Leading ,Trailing. If I give it numberoflines as 0 then it does not take any space.But if give it fix number of lines then it takes extra space from top & bottom. Please tell me what is the issue ?
Code for UITableView Delegates
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Constraints Given as
Screenshot for the UITableViewCell
When you give number of lines 0 along with the above constraints, the UILabel will have auto height, which means the AutomaticDimension for tableview cell will height relative to UILabel. When UILabel's text is empty, it's height becomes zero.
To overcome this situation, you can specify either of two things:
Minimum height for the UILabel (e.g. in the constraint give height > 30)
Estimated height for UITableViewCell using delegate method
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let lbl = cell.contentView.viewWithTag(10) as! UILabel
lbl.text = "abcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksdabcljfdslkfj;dlskfjd;slkfjds;ljdsfldjsf;ldsjfdslkfjds;lfjds;lfjdfl;jdsf;lkdjsf;ldsjf;ldskjd;slfjdsl;fjd;fljdfl;kjsdflljksd"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
I have two cells (one dynamic and other static) and I want to set different height for each cell like:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
/*tableView.dequeueReusableCell(withIdentifier: "Cell1", for: indexPath)
tableView.dequeueReusableCell(withIdentifier: "Cell2", for: indexPath)
if cell1 return 100
if cell2 return 20 */
}
It's possible to specific height for each cell not row.
How can I resolve this issue?
For static cell (created either as Xib or in storyboard), you can set the height like this, if you are displaying static cell in first row of your Table View.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 0 {
return 120
}
return UITableViewAutomaticDimension
}
To populate the static cell along with dynamic cell, You should do,
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datasource.count + 1
}
UITableViewAutomaticDimension
https://www.raywenderlich.com/129059/self-sizing-table-view-cells -
If you set correctly the autolayout for the cell you can use UITableViewAutomaticDimension to have the size without have to specify.
You only need to return a height for each indexPath. There is no need to dequeue a cell here. If you want different prototype cells you will do this in cellForRowAtIndexPath.
You can specify as many sections and rows as you want:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath.section {
case 0:
switch indexPath.row {
case 0:
return 100.0
case 1:
return 20.0
// Add rows here if needed
default:
return UITableViewAutomaticDimension
// Add sections here if needed
default:
default:
return UITableViewAutomaticDimension
}
}
*In, Swift 3.0
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else {
return 200
}
}
or you can set Constraint of your Cell proper and then write this code of your ViewDidLoad Method
yourTblView.estimatedRowHeight = 100
yourTblView.rowHeight = UITableViewAutomaticDimension*
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 100
} else {
return 20
}
}
I am trying to set the row height dynamically based on the content set in the detail text label, by using the below code in Part A.
I am inserting few lines of text into a cell's detail text label as shown below in Part B
I've looked at other similar questions but none have helped.
Can some one please advice how I can adjust the row height dynamically based on the content of the detail text label.
Part A
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Also tried
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Part B
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "daysIdentifier", for: indexPath)
cell.textLabel?.text = days[indexPath.row]
cell.detailTextLabel?.numberOfLines = 0
var joinedString = self.availabilityTimeDict[dayName]?.joined(separator: " \n ")
cell.detailTextLabel?.text = joinedString
return cell
}
Use custom cell and labels.
Set up the constrains for the UILabel. (top, left, bottom, right)
Set lines of the UILabel to 0
Add the following code in the viewDidLoad method of the ViewController:
tableView.estimatedRowHeight = 68.0
tableView.rowHeight = UITableView.automaticDimension
// Delegate & data source
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableView.automaticDimension;
}
Swift 4:
// Delegate & data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
Swift 4.2:
// Delegate & data source
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
give top,bottom,leading and trailing to your lable inside content of tableview.Use below methods of table view
func tableView(_ tableView: UITableView,heightForRowAt indexPath:IndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
return 100
}
First setup a custom cell and add a label and set its number of lines to zero and give bottom, top, leading, trailing constraints to cell's content view(dont give the height) also give a custom height to cell in size inspector then in viewDidLoad you just need to do,
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100.0