Skip to content

Instantly share code, notes, and snippets.

@tailang
Created September 21, 2017 12:20
Show Gist options
  • Save tailang/15f3f30dbc3a99570f008c6d38fc8677 to your computer and use it in GitHub Desktop.
Save tailang/15f3f30dbc3a99570f008c6d38fc8677 to your computer and use it in GitHub Desktop.
UITableViewAutomaticDimension height cahce
// this is the cell height cache; obviously you don't want a static size array in production
var itemHeights = [CGFloat](count: 1000, repeatedValue: UITableViewAutomaticDimension) 

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
  if itemHeights[indexPath.row] == UITableViewAutomaticDimension {
    itemHeights[indexPath.row] = cell.bounds.height
  }
  // fetch more cells, etc.
}

func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
  return itemHeights[indexPath.row]
}

但是没有解决动态高度,如某个文案 有一个less和more点击的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment