【问题标题】:tableview cell load all at once in iphone在 iphone 中一次性加载 tableview 单元格
【发布时间】:2009-09-17 22:26:21
【问题描述】:

我在我的应用程序中实现了以下代码。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    
NSString *CellIdentifier = [NSString stringWithFormat:@"%@%i",searchQueryString,indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    XML_SearchResult *t=[arrayResults objectAtIndex:indexPath.row];
    cell=((indexPath.row%2)==0) ? 
        [self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:NO] :
        [self getCellContentView:CellIdentifier drillTitle:t.drill_title stroke_type:t.stroke_type exercise_type:t.exercise_type level_name:t.level_name age_range:t.age_range alterNate:YES] ;
    CGRect a=[cell frame];
    UIImageView *bImg=[[UIImageView alloc] initWithFrame:a];
    bImg.image=[UIImage imageNamed:@"white-rect-tab-3.png"];
    [bImg setContentMode:UIViewContentModeScaleToFill]; 
    cell.selectedBackgroundView=bImg; [bImg release];
}
return cell;
}
-(UITableViewCell*)getCellContentView:(NSString*)cellIdentifier drillTitle:(NSString*)drillTitle stroke_type:(NSString*)stroke_type exercise_type:(NSString*)exercise_type level_name:(NSString*)level_name age_range:(NSString*)age_range alterNate:(BOOL)alterNate
{
CGRect label1Frame=CGRectMake(5, 20, 260, 32),label2Frame=CGRectMake(5, 62, 120, 30),label3Frame=CGRectMake(130, 62, 170, 30);//,BgFrame=CGRectMake(0, 0, 320, 60);
UITableViewCell *cell=[[[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, 312, 95) reuseIdentifier:cellIdentifier] autorelease];
UILabel *tmp; UIImageView *imgBg;
cell.backgroundColor=[UIColor clearColor];
// drill title label
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 1, 260, 32)]; 
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:@"ArialRoundedMTBold" size:18]];  tmp.text=drillTitle; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release]; 
// title label - tip
tmp=[[UILabel alloc] initWithFrame:label1Frame]; 
tmp.textColor=[UIColor colorWithRed:(9.0/255.0) green:(68.0/255) blue:(85.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:@"ArialRoundedMTBold" size:18]];  tmp.text=stroke_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// sub title - exercise type label - tip
tmp=[[UILabel alloc] initWithFrame:CGRectMake(5, 40 , 260, 32)]; 
tmp.textColor=[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];
[tmp setFont:[UIFont fontWithName:@"ArialRoundedMTBold" size:15]]; tmp.text=exercise_type; [tmp setShadowColor:[UIColor lightGrayColor]];
tmp.backgroundColor=[UIColor clearColor]; [cell.contentView addSubview:tmp]; [tmp release];
// Age - Range 
tmp=[[UILabel alloc] initWithFrame:label2Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:@"Age Range : %@",age_range];
tmp.textColor= [UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0];// : [UIColor blackColor] ;
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:@"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)]; [tmp setShadowColor:[UIColor lightGrayColor]];
[cell.contentView addSubview:tmp]; [tmp release];
// Difficulty leve - Range 
tmp=[[UILabel alloc] initWithFrame:label3Frame]; tmp.adjustsFontSizeToFitWidth=0; tmp.text=[NSString stringWithFormat:@"| Diff. Level : %@",level_name];
tmp.textColor=[UIColor blackColor];//[UIColor colorWithRed:(14.0/255.0) green:(105.0/255) blue:(128.0/255) alpha:1.0]; 
[tmp setShadowColor:[UIColor lightGrayColor]];
[tmp setBackgroundColor:[UIColor clearColor]];
[tmp setFont:[UIFont fontWithName:@"ArialMT" size:14]]; [tmp setShadowOffset:CGSizeMake(1,1)];
[cell.contentView addSubview:tmp]; [tmp release];
// arrow
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(280, 35, 13, 13)];  imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:@"arrow-2.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release]; 
// line
imgBg=[[UIImageView alloc] initWithFrame:CGRectMake(-4, 94, 298, 2)];  imgBg.contentMode=UIViewContentModeScaleToFill;
imgBg.image=[UIImage imageNamed:@"line.png"];
[cell.contentView addSubview:imgBg]; [cell.contentView sendSubviewToBack:imgBg]; [imgBg release]; 
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
return cell;
}

这就是动态自定义单元格的全部内容。

但问题是 - 第一次只创建可见单元格,然后 - 用户尝试向下滚动。将创建并显示其他可见的单元格。因此,第一次滚动 tableview 会有一些延迟。

解决办法是什么?

【问题讨论】:

    标签: iphone xcode uitableview


    【解决方案1】:

    为移动平台开发时,您需要关注内存占用。我觉得如果你试图一次加载 400 个单元格的图像,你可能会自找麻烦。这不是推荐的方法,您肯定会在加载时引入巨大的延迟。

    与其像这样进行黑客攻击,不如努力提高cellForIndexPath 函数的性能,使其加载更快。或异步。

    【讨论】:

    • 先生 - “所有数据都已经在数组中”意味着它没有问题。我只需将数组值加载到文本视图、标签和图像 - 将所有这些添加到单元格中。
    • 因此,根据您告诉我的内容,您还没有分配所有内存。您需要分配 400 个 UILabel、UIImage(也可能是 UIImageViews)以及 UIViews 和 UITableViewCell。你做错了,相信我。您应该只在任何给定时间在屏幕上显示单元格的 UI,并重用这些单元格。
    • 好的。让我再清楚一遍。请重新阅读我的问题。我在几分钟内做了一些修改。
    【解决方案2】:

    可能是图像加载需要花费所有时间。创建单元格不应该花费很长时间,尤其是如果您重复使用旧单元格。 如果图像通常相同,那么您可以缓存这些图像。缩放也需要时间,而且如果图像不是 PNG,它们将需要更长的时间来加载。 但对我来说,解决方案是在创建单元格时不加载图像,即不在:

    tableView:cellForRowAtIndexPath:
    

    而是实施:

    scrollViewDidEndDecelerating
    

    这是一个在表格停止滚动时调用的委托函数。然后,您可以获得可见单元格的列表 (indexPathsForVisibleRows) 并在每个单元格上设置图像。

    【讨论】:

      猜你喜欢
      • 2012-07-12
      • 2021-12-09
      • 2011-09-13
      • 2023-03-08
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多