简体   繁体   English

在 iOS 中显示多列表格的最有效方法是什么?

[英]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:我必须显示一个有 10 列的表格:

+-----+------+------+------+------+------+------+------+------+-------+ | 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.忽略表头(我不是显示它)我使用UITableView与子类UITableViewCell来表示数据。

My custom UITableViewCell has 10 UILabel subviews, all added to its' contentView .我的自定义UITableViewCell有 10 个UILabel子视图,全部添加到它的contentView One for each column.每列一个。

I simply set the text value for each UILabel subview in my cellForRowAtIndexPath method on the UITableView .我只是在UITableView上的cellForRowAtIndexPath方法中为每个UILabel子视图设置文本值。

At any one time there are about 14 rows on the screen.在任何时候,屏幕上大约有 14 行。 The scrolling is super fast.滚动速度超级快。 My reuseIdentifier is working nicely.我的reuseIdentifier运行良好。

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.比我使用UITableView其他屏幕慢很多,但那些列的数量要少得多。

My cells are all set to opaque (which I understand is the default anyway).我的单元格都设置为opaque (我理解这是默认设置)。

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:我已经运行了仪器,大部分时间(500 毫秒 74.5%)是:

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 .我正在使用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.启用 CoreData SQLDebug ,我可以看到 20 行的总提取执行时间为 0.0014 秒。 My batch size is set to 20 (slightly more than is visible on screen) to aid in scrolling.我的批量大小设置为 20(比屏幕上可见的略多)以帮助滚动。

Scrolling is super fast, so I think the data is not the issue here.滚动速度非常快,所以我认为数据不是这里的问题。

UITableViews are usually used for displaying one-column data. UITableViews 通常用于显示一列数据。 You can use UICollectionView for such things.您可以将UICollectionView用于此类事情。

Or, if you want to do it hard way you can implement your own table view based on scrollview .或者,如果您想以一种艰难的方式进行操作,您可以基于 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.尝试一次插入一行,假设在您的 viewDidLoad 或 viewwillappear 中,如果您尝试获取所有数据并进行一些计算,则需要时间。 So my suggestion is take one row data insert it, then another.所以我的建议是将一行数据插入它,然后再插入另一行。

You can try custom tableview sample: https://github.com/Viacheslav-Radchenko/TSUIKit您可以尝试自定义 tableview 示例: 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).主要的变化是我不再在每一行上使用 10 个不同的子视图(每列一个子视图)。 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.为了让每个单元格显示数据,我使用了附加到单元格contentView的子类UIView

In that subclassed UIView I've over written the drawRect method to use drawAtPoint:withAttributes: .在那个子类UIViewdrawAtPoint:withAttributes:drawRect方法以使用drawAtPoint:withAttributes: Calling this 10 times with different text and positions seems be much faster than positioning 10 different UILabel s.用不同的文本和位置调用 10 次似乎比定位 10 个不同的UILabel快得多。

This answer on StackOverflow pointed me in the right direction. StackOverflow 上的这个答案为我指明了正确的方向。

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).另一个较小的更改是缓存NSClassFromString(myCustomCellName)的结果,每次为表中的每一行调用新的结果(当然除非它被重用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在iOS上显示CVImageBufferRef的最有效方法是什么 - What is the most efficient way to display CVImageBufferRef on iOS 使用数据,iOS,Swift 3刷新表的内存效率最高的方法是什么? - What is the most memory efficient way to refresh table with data, iOS, Swift 3? 在iOS中追踪时间的最有效/最精确的方法是什么? - What's the most efficient / most precise way to track time in iOS? 在swift和IOS中使用标题创建表选择视图的最有效方法 - Most efficient way of creating a table select view, with headings in swift & IOS 在iOS上缩小图像的内存效率最高的方法是什么? - What is the most memory-efficient way of downscaling images on iOS? 在iOS上将高清视频缩减为SD的最快,最有效的方法是什么? - What is the fastest and most efficient way on iOS to downscale a HD video to SD? 在iOS中获取像素矩形的最有效方法是什么? - What is the most efficient way to grab a rectangle of pixels in ios? 在 SceneKit iOS 中渲染流式视频的最有效方法是什么? - What is the most efficient way to render a streamed video in SceneKit iOS? 一次登录iOS-最有效的方式? - One time login in iOS - Most Efficient Way? 查询Firebase iOS的最有效方法 - Most Efficient Way to Query Firebase iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM