Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift UITableView set rowHeight

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)

like image 719
tim_yng Avatar asked Sep 02 '14 21:09

tim_yng


People also ask

How do I change cell height in Swift?

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.

How do I populate UITableView?

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.

How can I group tableView items from a dictionary in Swift?

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.


2 Answers

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.

like image 96
Nagarjun Avatar answered Oct 18 '22 23:10

Nagarjun


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 
like image 27
Mundi Avatar answered Oct 18 '22 23:10

Mundi