Jason McGraw
Jason McGraw

Reputation: 331

To dynamically add UIViews, or create a UITableView? ios

I'm new to iOS development. I'm currently working on an app that has dynamic content that is brought back from a web server, mapped into data models, and displayed to the user.

So far, I have created views to consume the models, and then add the views as sub views into a scroll view, calculating y coordinates for each so that they are added one under another.

This sequential adding of elements is "list like", and could be table cells in a table view. Thus far I've avoided this approach, because some of my views have varying heights, and I've read that performance on table views declines significantly when cells have dynamic row heights.

Do I risk performance adding sequential subviews into a scroll view? Is a tableview a better solution for what I'm describing?

Upvotes: 2

Views: 319

Answers (2)

David Ogren
David Ogren

Reputation: 4800

The reason that table views performance can degrade with dynamic cell heights is merely because the system must call -tableView:heightForRowAtIndexPath: for every single cell in order to figure out the scroll bars properly. (Although this changes in iOS7 with the addition of the -tableView:estimatedHeightForRowAtIndexPath: method. Those methods would allow you to provide quick and dirty estimate heights for non visible cells.)

In any case, it means scroll views will have the same performance problem: you will have to calculate the height of every single subview as you add it to the scroll view. (As you already mentioned.) In fact, table view's ability to load cells on demand would give you a lot of advantages performance wise over a stock scroll view.)

In general, you should pick the approach that makes the most sense for you. Scroll views are essentially a blank canvas where you "roll your own". Table views give you a lot of functionality "out of the box". If they are lists, I'd lean towards table views.

Upvotes: 2

Mika
Mika

Reputation: 5845

You should definitely use a table view. You can use the heightForRowAtIndexPath to manage the row height.

Upvotes: 1

Related Questions