Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: How to reload row height in UITableViewCell without reloading data

I have a case in which I have to reload only height of a UITableViewCell.

but if I call the function

tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: webView.tag, inSection: 0)], withRowAnimation: .Automatic) 

it reloads the height as well as the data of the cell. How can i just control the height of cell in Swift?

This is my cellForRow block :

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {     if indexPath.row == 0 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1     cell.heading.text = headerText         return cell     }      else if indexPath.row == 1 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell2", forIndexPath: indexPath) as! CustomTableViewCell2         ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in             cell.mainImage.image = image         })         return cell     }     else if indexPath.row == 2 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell3", forIndexPath: indexPath) as! CustomTableViewCell3         //cell.aurthorImage.image = UIImage(named : "obama")         ImageLoader.sharedLoader.imageForUrl(self.headerImage , completionHandler:{(image: UIImage?, url: String) in             cell.aurthorImage.image = image         })         cell.aurthorImage.tag = aurthorID         cell.aurthorImage.layer.cornerRadius = cell.aurthorImage.frame.height/2         cell.aurthorImage.clipsToBounds = true         cell.aurthorImage.userInteractionEnabled = true         cell.aurthorImage.addGestureRecognizer(aurthorImageTapRecignizer)         cell.aurthorName.text = self.authorName         cell.time.text = self.time         self.followButton = cell.followButton         return cell     }      else if indexPath.row == 3 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell4", forIndexPath: indexPath) as! CustomTableViewCell4         let htmlHeight = contentHeights[indexPath.row]          cell.webElement.tag = indexPath.row         cell.webElement.delegate = self         cell.webElement.loadHTMLString(HTMLContent, baseURL: nil)         cell.webElement.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)          return cell     }     else if indexPath.row == 4 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1         cell.heading.text = "Related Posts"         return cell     }     else if indexPath.row == 5{         let cell = tableView.dequeueReusableCellWithIdentifier("cell6", forIndexPath: indexPath) as! CustomTableViewCell6         return cell      }     else if indexPath.row == 6 {         let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1         cell.heading.text = "Comments"         return cell     }     else if indexPath.row == 7 {          let cell = tableView.dequeueReusableCellWithIdentifier("cell5", forIndexPath: indexPath) as! CustomTableViewCell5           let htmlHeight = contentHeights[indexPath.row]         self.commentSection = cell.commentsView         self.commentSection.tag = indexPath.row         self.commentSection.delegate = self         let url = NSURL(string: commentsURL)         let requestObj = NSURLRequest(URL: url! )         self.commentSection.loadRequest(requestObj)         self.commentSection.frame = CGRectMake(0, 0, cell.frame.size.width, htmlHeight)             commentSectionDidNotLoad = false              return cell     }     else {         let cell = tableView.dequeueReusableCellWithIdentifier("cell1", forIndexPath: indexPath) as! CustomTableViewCell1         cell.heading.text = headerText         return cell     } 
like image 791
Talha Ahmad Khan Avatar asked May 24 '16 07:05

Talha Ahmad Khan


People also ask

How do you refresh a tableView cell in Swift?

You can use self. tableView. reloadData() in order to reload your entire table or self.

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. We'll see this with the help of an sample project.


1 Answers

You can use this code to update the cell's height without reloading their data:

tableView.beginUpdates() tableView.endUpdates() 

You can also use this method followed by the endUpdates method to animate the change in the row heights without reloading the cell.

UITableView Class Reference

like image 148
Diogo Antunes Avatar answered Sep 21 '22 16:09

Diogo Antunes