xcode UITableView:加载所有单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12268812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UITableView: load all cells
提问by Michael
Is it possible to load all cells of an UITableView when the view is loaded so that they are not loaded when I'm scrolling? (I would show a loading screen while doing this)
是否可以在加载视图时加载 UITableView 的所有单元格,以便在我滚动时不加载它们?(我会在执行此操作时显示加载屏幕)
Please, it's the only way at my project (sorry too complicate to explain why ^^)
拜托,这是我项目的唯一方法(抱歉太复杂了,无法解释原因^^)
EDIT:
编辑:
Okay let me explain you, what I'm definite doing:
好吧,让我解释一下,我肯定会做什么:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [NSString stringWithFormat:@"Identifier %i/%i", indexPath.row, indexPath.section];
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSDictionary *currentReading;
if (cell == nil)
{
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
UILabel *label;
UIView *separator;
if(indexPath.row == 0)
{
// Here I'm creating the title bar of my "table" for each section
}
else
{
int iPr = 1;
do
{
currentReading = [listData objectAtIndex:iPr-1];
iPr++;
} while (![[currentReading valueForKey:@"DeviceNo"] isEqualToString:[devicesArr objectAtIndex:indexPath.section]] ||
[readingresultsArr containsObject:[currentReading valueForKey:@"ReadingResultId"]]);
[readingresultsArr addObject:[currentReading valueForKey:@"ReadingResultId"]];
//
// ...
//
}
}
return cell;
}
My error happens in the do-while-loop: "listData" is an array with multiple dictionaries in it. My problem ist that when I'm scrolling my table slowly down, all is fine, but when I'm scrolling quickly to the end of the view and then I'm scrolling to the middle, I get the error that iPr is out of the array's range. So the problem is, that the last row of the first section has already been added to the "readingresultsArr", but has not been loaded or wants to be loaded again. That's the reason why I want to load all cells at once.
我的错误发生在 do-while 循环中:“listData”是一个包含多个字典的数组。我的问题是,当我慢慢向下滚动我的表格时,一切都很好,但是当我快速滚动到视图的末尾然后我滚动到中间时,我得到了 iPr 不在的错误数组的范围。所以问题是,第一部分的最后一行已经添加到“readingresultsArr”中,但尚未加载或想要再次加载。这就是我想一次加载所有单元格的原因。
回答by Kent
You can cause all of the cells to be pre-allocated simply by calling:
您可以通过调用以下命令来预先分配所有单元格:
[self tableView: self.tableView cellForRowAtIndexPath: indexPath];
for every row in your table. Put the above line in an appropriate for-loop and execute this code in viewDidAppear
.
对于表格中的每一行。将上面的行放在适当的 for 循环中并在viewDidAppear
.
The problem however is that the tableView will not retain all of these cells. It will discard them when they are not needed.
然而问题是 tableView 不会保留所有这些单元格。当不需要它们时,它会丢弃它们。
You can get around that problem by adding an NSMutableArray
to your UIViewController
and then cache all the cells as they are created in cellForRowAtIndexPath
. If there are dynamic updates (insertions/deletions) to your table over its lifetime, you will have to update the cache array as well.
您可以通过向NSMutableArray
您添加UIViewController
,然后缓存在cellForRowAtIndexPath
. 如果您的表在其生命周期内动态更新(插入/删除),您也必须更新缓存数组。
回答by chings228
put a uitableview on a uiscrollview
将 uitableview 放在 uiscrollview 上
for example , you expect the height of the full list uitableview is 1000
例如,您希望完整列表 uitableview 的高度为 1000
then set the uiscrollview contentsize is 320X1000 and set the uitableview height is 1000
然后设置uiscrollview contentsize为320X1000,设置uitableview高度为1000
then all cell load their content even not visible in screen
然后所有单元格加载它们的内容,即使在屏幕上也不可见
回答by Lina
in my case it was that I used automaticDimension for cells height and put estimatedRowHeight to small that is why tableview loaded all cells
就我而言,我使用了automaticDimension作为单元格高度并将estimatedRowHeight设置为小,这就是tableview加载所有单元格的原因
回答by Andrew
Following the comment that was made by juancazalla, I found that if you have a tableView that is using automaticDimension, loading all the cells at once can be best achieved by setting estimatedRowHeight to a low value (such as 1).
根据 juancazalla 的评论,我发现如果您有一个使用 automaticDimension 的 tableView,最好通过将 EstimatedRowHeight 设置为较低的值(例如 1)来一次加载所有单元格。