我遇到了我的tableview的加载时间问题,我该如何改进呢?加载约90000行以上的表格视图大约需要25-30秒。我在我的sqlite数据库中有大约130000行,我使用核心数据访问数据。Tableview加载缓慢
任何帮助或建议将不胜感激。
谢谢你的时间,如果你需要更多的信息,请让我知道。
我一直试图优化的两件事。
+ 核心数据 - 也许我提取的数据可以改进,但我不知道我能做什么,下面列出的代码。
if (fetchedResultsController != nil)
{
return fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"DataModel" inManagedObjectContext:_context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"subsection" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
//[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"section == [c] %@ AND area == 'Tables'", tablesSection]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"area == 'Tables'"]];
[fetchRequest setFetchBatchSize:100];
//[fetchRequest setFetchLimit:100];
[fetchRequest setFetchOffset:10];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:@"uppercaseFirstLetterOfNameTables" cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
fetchedResultsController.delegate = self;
return fetchedResultsController;
+ 的实现代码如下,我想我在加载数据的方式可能是主要的问题在这里。我动态地计算每个单元格的高度,我认为这会减慢速度,如果我删除方法heightForRowAtIndexPath的代码,我可以将加载时间降低到8秒。
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
Model *info;
if(self.searchDisplayController.isActive)
{
info = [searchFetchedResultsController objectAtIndexPath:indexPath];
}
else
{
info = [fetchedResultsController objectAtIndexPath:indexPath];
}
NSString *subsectionHeight = info.subsection;
NSString *textHeight = info.text;
CGSize titleSize = [subsectionHeight sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:20] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [textHeight sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:16] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return detailSize.height+titleSize.height;
//return 88;
}
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
cell.detailTextLabel.numberOfLines = 0;
}
//if-statement need to prevent search index issue
if(self.searchDisplayController.isActive && [self.searchDisplayController.searchBar.text isEqualToString:@""])
{
return cell;
}
// Configure the cell...
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
(void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
Model *info;
if (self.searchDisplayController.isActive)
{
info = [searchFetchedResultsController objectAtIndexPath:indexPath];
}
else
{
info = [fetchedResultsController objectAtIndexPath:indexPath];
}
cell.textLabel.text = info.subsection;
cell.detailTextLabel.text = info.text;
}
更新:我使用与heightForRowAtIndexPath解决注释掉了,但我需要与搜索优化帮助,可能有人帮助我了解如何实施该解决方案在WWDC 2010大会137优化核心数据的表现,我不明白创建一个新的实体部分,例如我是否需要用数据填充新的实体?
对于单个表视图,90000行是荒谬的。没有人会永远能够滚动到最后。把你的数据分成不同的部分,并有一个导航结构。 – jrturton 2012-04-03 09:22:13
我同意单个tableview的90000行是疯狂的,但不幸的是,这是我给的,我很想打破数据,但我不知道如何做到这一点,因为它的SAP表数据。 – JingJingTao 2012-04-03 10:00:36