Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewAutomaticDimension not working until scroll

When the Table View is first loaded, all of the visible cells are the estimatedRowHeight. As I scroll down, the cells are being automatically sized properly, and when I scroll back up the cells that were initially estimatedRowHeight are being automatically sized properly.

Once the cells are being automatically sized, they don't ever seem to go back to being the estimatedRowHeight.

override func viewDidLoad() {     super.viewDidLoad()     self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)     self.tableView.estimatedRowHeight = 80.0     self.tableView.rowHeight = UITableViewAutomaticDimension      // Uncomment the following line to preserve selection between presentations     // self.clearsSelectionOnViewWillAppear = false      // Uncomment the following line to display an Edit button in the navigation bar for this view controller.     // self.navigationItem.rightBarButtonItem = self.editButtonItem() } 

and

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {      let cellIdentifier = "Cell"     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as CustomTableViewCell      // Configure the cell...     let restaurant = restaurants[indexPath.row]     cell.namelabel.text = restaurant.name     cell.locationlabel.text = restaurant.location     cell.typelabel.text = restaurant.type     cell.thumbnailImageView.image = UIImage(named: restaurant.image)     cell.thumbnailImageView.layer.cornerRadius = cell.thumbnailImageView.frame.size.width / 2     cell.thumbnailImageView.clipsToBounds = true     cell.accessoryType = restaurant.isVisited ? .Checkmark : .None      return cell } 

Thoughts on how to have the cells autoresize initially?

UPDATE: As of Xcode 7 beta 6 this is no longer an issue

like image 396
jjatie Avatar asked Jan 08 '15 14:01

jjatie


1 Answers

Just call "reloadSections" after your Table is loaded:

self.tableView.reloadSections(NSIndexSet(indexesInRange: NSMakeRange(0, self.tableView.numberOfSections())), withRowAnimation: .None) 

Or in Swift 3:

let range = Range(uncheckedBounds: (lower: 0, upper: self.tableView.numberOfSections)) self.tableView.reloadSections(IndexSet(integersIn: range), with: .none) 
like image 168
Blank Avatar answered Oct 03 '22 10:10

Blank