简体   繁体   中英

What is the most efficient way to display a table with multiple columns in iOS?

My problem is with slow loading, the scrolling speed is fast.

I have to display a table that has 10 columns:

+-----+------+------+------+------+------+------+------+------+-------+ | Pos | Name | Col3 | Col4 | Col5 | Col6 | Col7 | Col8 | Col9 | Col10 | +-----+------+------+------+------+------+------+------+------+-------+ | 1 | Foo | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 2 | Bar | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | | 3 | Baz | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | +-----+------+------+------+------+------+------+------+------+-------+

Ignoring the table header (I'm not displaying it) I'm using a UITableView with a subclassed UITableViewCell to represent the data.

My custom UITableViewCell has 10 UILabel subviews, all added to its' contentView . One for each column.

I simply set the text value for each UILabel subview in my cellForRowAtIndexPath method on the UITableView .

At any one time there are about 14 rows on the screen. The scrolling is super fast. My reuseIdentifier is working nicely.

However, the initial load of the table is noticeably slow. A lot slower than my other screens that use a UITableView but those have substantially less columns.

My cells are all set to opaque (which I understand is the default anyway).

Is there a trick I'm missing? Are there too many views? Is there another approach I should take? Or is what I'm doing fine and should I be looking for another issue in my code effecting the load time?

Update:

I have run instruments and the bulk of the time (500ms 74.5%) is:

CA::Layer::layout_and_display_if_needed(CA::Transaction*)

Update:

A few people have suggested my data is the bottle neck.

I am using a NSFetchedResultsController .

There are no calculations. The data for each row is loaded directly from the corresponding Entity and data is displayed as is . I am not walking the object graph to get data from any other relationships or anything like that.

With CoreData SQLDebug turned on I can see total fetch execution time is 0.0014s for 20 rows. My batch size is set to 20 (slightly more than is visible on screen) to aid in scrolling.

Scrolling is super fast, so I think the data is not the issue here.

UITableViews are usually used for displaying one-column data. You can use UICollectionView for such things.

Or, if you want to do it hard way you can implement your own table view based on scrollview .

But about slow loading - do you load data from some web-service or is it just simple demo with all the same values for all data?(If not - try this way to understand problem)

Try to insert one row at a time, suppose in your viewDidLoad or viewwillappear, if you are trying to fetch all the data and doing some calculation then it will take time. So my suggestion is take one row data insert it, then another.

You can try custom tableview sample: https://github.com/Viacheslav-Radchenko/TSUIKit

Hope, It will may helpful to you,

:)

I've halved the time it takes for the screen to load now through a few changes.

The major change is I no longer use 10 different subviews on each row (it was one subview for each column). Instead, I now just use one subview per cell.

To get the data to display for each cell I'm using a subclassed UIView attached to the contentView of the cell.

In that subclassed UIView I've over written the drawRect method to use drawAtPoint:withAttributes: . Calling this 10 times with different text and positions seems be much faster than positioning 10 different UILabel s.

This answer on StackOverflow pointed me in the right direction.

Another, smaller, change was to cache the result of NSClassFromString(myCustomCellName) which was being called fresh each time for every row in the table (unless of course it was being re-used).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM