Rawan
Rawan

Reputation: 1639

Edit the row height for tableview Swift

In the tableView I am having a web view, and the height cell should be equal to the webView, i have tried to edit the row height of the cell by the following code:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    let cell = tableView.cellForRowAtIndexPath(indexPath) as ActivitiesCell

    return cell.webView.scrollView.contentSize.height
}

But i got this exception when initializing the var cell : "Thread 1:EXC_BAD_ACCESS(code=2,address=0x306d2c): so how can i make the height of the cell dynamic depending on the webview height ? thanks.

Upvotes: 0

Views: 941

Answers (1)

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

The reason for EXC_BAD_ACCESS is that you are trying to access the cell inside tableView's heightForRowAtIndexPath: method. The cell is not created till yet and does not exist.

The tableview calls heightForRowAtIndexPath: method followed by cellForRowAtIndexPath: to create the cell.

First heightForRowAtIndexPath: is called to get the height of the cell at index path. After this cellForRowAtIndexPath: is called in which cell is created for the corresponding index path.

As cell is created in cellForRowAtIndexPath: and heightForRowAtIndexPath: is called before this so you cannot access a cell there(as it is not created yet, otherwise EXC_BAD_ACCESS as above). And in order to return the dynamic height of the cell for the corresponding index path you should calculate the the height on the basis of data modal rather than trying to access the cell.

Upvotes: 3

Related Questions