Giter Site home page Giter Site logo

bradhowes / vhtc Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 5.32 MB

Simple example of how to properly configure iOS UITableView cells containing UITextView elements.

License: MIT License

Swift 100.00%
uitableviewcell uitableviewcontroller uitableview ios

vhtc's Introduction

Variable Height UITextView in UITableView Cells

The project shows how to properly create UITableView cells with UITextView elements so that the cells quickly and properly adjust their height to account for the UITextView content while honoring existing Auto Layout constraints. The animation below shows how fast the resizing can be with a table containing 10,000 UTextView cells of varying content.

Approach

The class FullTextView is a derivative of UITextView which simply keeps up-to-date its intrinsicContentSize so that it matches the size obtained by its internal NSLayoutManager. Doing so allows Auto Layout to proprerly adjust surrounding cells to honor their constraints, and for the parent view to update its size to match that of its content.

The class Cell is a derivative of UITableViewCell which offers a method cellHeightForContent that properly queries the cell's content view for its best layout size. This is the value that will be the basis for the answer to the table view's question, "How tall is this cell?"

I created two height calculation strategies to help with measuring performance, especially with large number of items:

  • EstimatedHeightCalculatingStrategy - provides UITableView with an constant estimate of row height, and then an accurate value later when asked by UITableView.

  • CachedHeightArrayStrategy - calculates row heights up-front, always returning an accurate value to UITableView.

The first approach is by far the fastest for very large cell counts. Using a background thread to help with the recalculations might help the second approach but I'm not sure the complexity is really worth the effort given how fast and simple the first strategy is. For anything above a cell count of 500 or so, the second approach demands too much time before the table appears to the user.

License

This project is licensed under the terms of the MIT license.

vhtc's People

Contributors

bradhowes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

endeavor5

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.