I am trying to set the height of each row in the tableView
to the height of the corresponding cell with this code:
override func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat { var cell = tableView.cellForRowAtIndexPath(indexPath) return cell.frame.height }
I get this error when initialising var cell
:
Thread 1:EXC_BAD_ACCESS(code=2,address=0x306d2c)
To change the height of tableView cell in ios dynamically, i.e resizing the cell according to the content available, we'll need to make use of automatic dimension property.
There are two main base ways to populate a tableview. The more popular is through Interface Building, using a prototype cell UI object. The other is strictly through code when you don't need a prototype cell from Interface Builder.
An easier way to solve this problem is to copy your dictionary into a temporary variable. Use removeFirst to extract the values from the array inside the dictionary. Show activity on this post.
For setting row height there is separate method:
For Swift 3
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 100.0;//Choose your custom row height }
Older Swift uses
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { return 100.0;//Choose your custom row height }
Otherwise you can set row height using:
self.tableView.rowHeight = 44.0
In ViewDidLoad.
Put the default rowHeight
in viewDidLoad
or awakeFromNib
. As pointed out by Martin R., you cannot call cellForRowAtIndexPath
from heightForRowAtIndexPath
self.tableView.rowHeight = 44.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With