简体   繁体   English

UITableView中的可见单元格数

[英]Number of visible cells in UITableView

I have A UITableView that is slightly smaller than its parent view. 我有一个比其父视图略小的UITableView The height of each cell is the same as the height of the table (only one cell is fully visible at a time). 每个单元格的高度与表格的高度相同(一次只能看到一个单元格完全可见)。

The UITableView has clipsToBounds set to NO to be able to see the top of the next cell. UITableViewclipsToBounds设置为NO ,以便能够看到下一个单元格的顶部。

But the next cell is not loaded in visibleCells as it doesn't fit in the UITableView . 但是下一个单元格没有加载到visibleCells因为它不适合UITableView When I start to scroll it appears (or if I set the cell height to be 1 less pixel). 当我开始滚动它时(或者如果我将单元格高度设置为1个像素)。 How can I make the UITableView to always have 2 cells loaded instead of only the one that fits in the table? 如何让UITableView始终加载2个单元格,而不是只加载适合表格的单元格?

视图层次结构

On this illustration, the second UITableViewCell is not loaded by default and loads only when I start to scroll. 在此图示中,默认情况下未加载第二个UITableViewCell,仅在我开始滚动时加载。

PS: I'm also using pagingEnabled on the table view. PS:我也在表视图上使用了pagingEnabled

You could make the tableview bigger than a single cell, then set the tableview content insets so that the top and bottom cells appear in the correct places at max/min scroll. 您可以使tableview大于单个单元格,然后设置tableview内容插入,以便顶部和底部单元格以最大/最小滚动显示在正确的位置。 You could also put an invisible view over the portion of the tableview that is not supposed to be there (the bigger part that you don't currently have there) so that this part of the tableview is still not selectable. 你也可以在tableview的那个不应该存在的部分放置一个不可见的视图(你当前没有那个更大的部分),这样tableview的这部分仍然是不可选择的。

It could be a bit of work, but the only way to ensure that the cell is drawn is to make it on screen. 这可能有点工作,但确保绘制单元格的唯一方法是在屏幕上显示它。

In further research, this guy says the same thing, but for a different reason. 在进一步的研究中, 这个人说同样的话,但出于不同的原因。

2. UITableView buffering 2. UITableView缓冲

This may be one of the simplest changes you can make to improve perceived performance in any iOS application. 这可能是您可以在任何iOS应用程序中提高感知性能的最简单更改之一。 Simply enough, if you want to preload data of you table view beyond the visible bounds of the table view, you need only extend the table view itself to achieve the desired buffer. 简单地说,如果要将表视图的数据预加载到表视图的可见边界之外,则只需扩展表视图本身即可实现所需的缓冲区。 For example, you have an iPhone app that has a full screen table view (320x480 or 320x568). 例如,您有一个具有全屏桌面视图(320x480或320x568)的iPhone应用程序。 If you want the table view to buffer an extra table view height of data, you just make your table view twice as tall (320x960 or 320x1136) and then counter the height extension using UIScrollView's contentInset and scrollIndicatorInsets properties. 如果希望表视图缓冲数据的额外表视图高度,则只需将表视图设置为两倍高(320x960或320x1136),然后使用UIScrollView的contentInset和scrollIndicatorInsets属性计算高度扩展。 It really is that easy. 它真的很容易。 With this simple change to your table views, you now have the ability to avoid showing table view cells with blank content as the content is downloaded and then have the content jarringly show up. 通过对表视图的这种简单更改,您现在可以避免在下载内容时显示包含空白内容的表视图单元格,然后让内容显示出来。

in viewDidAppear or viewWillAppear viewDidAppearviewWillAppear

for (int i=0 ; i <10 ; i ++) {
    [self tableView: self.myTableView cellForRowAtIndexPath: indexPath];
     // give your indexpath for section=0 and row=i; 
}

this line will Allocate all UITableViewCell but UITableView will discard them if they are not in Visiblecells. 这一行将分配所有UITableViewCell但如果它们不在Visiblecells中, UITableView将丢弃它们。

To Do it add all your cells in NSMutableArray and in cellForRowAtIndexPath: load them from NSMutableArray. 要执行此操作,请在NSMutableArraycellForRowAtIndexPath:添加所有单元格cellForRowAtIndexPath:从NSMutableArray加载它们。

- (UITableViewCell *)tableView:(UITableView *)tableView 
        cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
    return [preCompiledCellArray objectAtIndex:indexPath.row]; 
}

Unless there are a lot of rows or you need some table-like feature you have not mentioned, you might be a lot happier not using UITableView at all here. 除非有很多行,或者你需要一些你没有提到的类似于表的功能,否则你可能会更乐意不使用UITableView。 What you are describing sounds much more like an ordinary UIScrollView. 您所描述的内容听起来更像普通的UIScrollView。

Suppose, for example, there are just four "cells". 例如,假设只有四个“细胞”。 Then this is just an ordinary UIScrollView and you can populate its entire content (the four "cells") beforehand. 然后这只是一个普通的UIScrollView,您可以预先填充其整个内容(四个“单元格”)。

On the other hand, if there are 100 "cells", you could run out of memory that way. 另一方面,如果有100个“单元格”,那么你可能会耗尽内存。 But you can still remove subviews when they are no longer visible and create them as needed, as the user scrolls; 但是,当用户不再可见时,您仍然可以删除子视图,并在用户滚动时根据需要创建子视图; see the WWDC 2010 video on scroll views for how to manage that. 有关如何管理它的滚动视图,请参阅WWDC 2010视频。

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

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