2009-05-05 41 views
3

检查出[返回单元格] ..前的最后行的图片加载后,滚动速度decreasing..it似乎滚动卡住如何在每个单元格中加载图像时提高表格视图中滚动的速度?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    NSString *itemDescription=[[stories objectAtIndex: storyIndex] 
               objectForKey:@"title"]; 

    CGRect aframe = CGRectMake(80, 30, 250, 40); 
    textLabel = [[UILabel alloc] initWithFrame:aframe]; 
     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    textLabel.numberOfLines=0; 
    textLabel.textColor = [UIColor darkTextColor]; 
    textLabel.backgroundColor = [UIColor whiteColor]; 
    [cell.contentView addSubview:textLabel]; 
    textLabel.text=itemDescription; 

    CGRect frame = CGRectMake(0, 0, 70,80); 
    UIImageView *TopImageView = [[UIImageView alloc] init]; 
    [cell.contentView addSubview:TopImageView]; 
    TopImageView.frame=frame; 

    m_strImage = [m_imglinkArray objectAtIndex:storyIndex]; 

    TopImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:m_strImage]]]; 
    TopImageView.image=TopImage; 

    return cell; 
} 

你们能帮我提高速度滚动?

问候

阿伦

+0

你应该改善格式的你,因为它无法读取的问题... – 2009-05-05 06:59:53

回答

0

看来你是从一个url每个表视图要求得到小区一排时加载。这发生在主应用程序线程(用户界面线程)中,并且它阻止了用户界面,使其无法响应。

特别是如果这个URL是你从互联网加载资源,那么这个停顿是很大的。

我建议您应该加载在后台线程中的图像,并显示图像占位符,直到他们从网上下载。

你可以试试下面的网址提供的解决方案......

http://www.markj.net/iphone-asynchronous-table-image/

2

我相信你可以有两个问题在这里。首先是没有多线程图像加载的问题,其次是你如何使用UITableViewCell。

每当一个细胞出现在屏幕上的iPhone电话的cellForRowAtIndexPath - 如果你向下滚动,高于一切被卸载,当你再次滚动备份重新加载。正如已经指出的那样,这可以通过多线程模式解决 - 或者将链接发布到markj.net,和/或使用NSThread。 NSThread的优点是您可以更灵活地处理问题或加载更多的图像数据。 (我会同时使用)

为了做到这一点,你有效地需要有一个自定义的UITableViewCell。无论如何,这很好,因为您可以让表格单元对其内容负责而不是视图控制器,并且可以高效地操作单元格格式和内容。这是一个伟大的一个链接约UITableCellView:http://blog.atrexis.com/index.cfm/2009/1/6/iPhone--Customize-the-UITableCellView

当你实现的UITableViewCell,尽最大努力把所有addSubView和格式电话中的“如果(细胞==无)”块,只有如果你不能把它们放在你的自定义initWithFrame中。您会发现iPhone正在堆叠您添加的每个子视图 - 它不会清除旧视图。通过将所有背景颜色设置为“UIColor clearColor”,然后在点击之间改变文字,您可以在工作中看到这一点 - 您将看到一堆文字在彼此之上。这通常不可见,因为固体填充背景覆盖了所有“旧”子视图。

在结合这两种方法之前,考虑实施“模型”。你有一个视图和一个控制器,但是你的所有数据都应该在模型中。所有的图片网址,内容等都应该像NSMutableArray,自己的自定义对象,或者通过SQLite。一旦你有一个模型,你可以实现你的图像缓存。通过缓存,所有图像将在您的应用程序负载之间保留,这将节省电池,带宽和时间。

我会: 1.把你所有的数据放在某种模型中(NSMutableArray,SQLLite,something) 2。实现自己的自定义的UITableViewCell 3.确保你已经0 addSubView,的cellForRowAtIndexPath 4.使用代表团例子或NSThread的积极块在后台加载的所有图像内init或ALLOC电话 5.添加本地图片缓存

在关于缓存图像的Apple论坛中,有一些很好的例子。

2

nessence有很多很好的信息。更多的想法:

你在这里疯狂地泄漏。你创建的每个单元格都会泄漏2个UILabels,一个UIImageView和一个UIImage。

如前所述,您不仅会泄漏这些内容,而且还会因为您使用addSubview将其中一个放在另一个之上而积累在视图中。

在单元格绘制期间接触到网络非常慢,并且阻止了您的用户界面。如果这些URL是本地的,那么您可以使用UIImage的+ imageWithContentsOfFile,如果不是,则需要在后台加载。

我不认为你需要一个线程在这里。 NSURLConnection是一种在后台加载数据而不会产生线程开销的好方法。

nessence是完全正确的,你需要一个Story的模型类。

您可重用单元配置的基本方法不正确。您不会获取可重用的单元格,然后向其添加子视图。所有的子视图都应该添加到if()块中以创建单元格。然后,在每一遍中,你只需改变事物的价值。我已经重写了下面的一些代码来演示。这仍然是不正确的代码,因为它在单元格绘制期间触及网络,这可能是太多元素在子视图单元格中(而不是自定义单元格),但它更接近正确的想法。我甚至不知道这是否编译;我只是在这里输入。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     // Here we do all our cell creation 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

     // Make the label 
     CGRect aframe = CGRectMake(80, 30, 250, 40); 
     UILabel *textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; // Note the -autorelease so we don't leak 

     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     textLabel.numberOfLines=0; 
     textLabel.textColor = [UIColor darkTextColor]; 
     textLabel.backgroundColor = [UIColor whiteColor]; 
     textLabel.tag = DescriptionTag; // A tag so we can find it later (you'll need a constant for this) 
     [cell.contentView addSubview:textLabel]; 

     // And the second label 
     aframe = CGRectMake(80, 30, 250, 40); 
     textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; 
     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     textLabel.numberOfLines=0; 
     textLabel.textColor = [UIColor darkTextColor]; 
     textLabel.backgroundColor = [UIColor whiteColor]; 
     textLabel.tag = TitleTag; 
     [cell.contentView addSubview:textLabel]; 

     // The image view 
     CGRect frame = CGRectMake(0, 0, 70,80); 
     UIImageView *topImageView = [[[UIImageView alloc] init] autorelease]; 
     topImageView.frame = frame; 
     topImageView.tag = TopImageTag; 
     [cell.contentView addSubview:topImageView]; 
    } 

    // all the above was cell creation; we do that as seldom as possible. 
    // Now we do cell configuration. We want this to be fast. 

    UILabel *descriptionLabel = (UILabel*)[cell.contentView viewWithTag:DescriptionTag]; 
    descriptionLabel.text = itemDescription; 

    UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:TitleTag]; 
    titleLabel.text =[[stories objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    NSString *imageURLString = [m_imglinkArray objectAtIndex:storyIndex]; // You should have a model class called Story, not two arrays. 
    UIImage *image = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]] autorelease]; // This is still way too slow if it's a remote URL 
    UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:TopImageTag]; 
    imageView.image = image;  

    return cell; 
} 

我建议你花一些时间学习TableViewSuitePractical Memory ManagementCoding Guidelines for Cocoa。一段时间研究Cocoa的基础知识也会很有用,因为这里的编码风格表明你可能没有坚实的基础。虽然这是一本Mac书,但我仍然推荐Cocoa Programming for Mac OS X。如果你有兴趣使用这个来学习iPhone,我已经编制了一个syllabus这可能会有所帮助。我还没有审查过,但斯坦福的在线CS193P course看起来很有希望。

1

这个问题在这里已经问了好几次了,你需要做的是异步加载图像为了防止滚动速度减慢。

这就是所谓的“懒图像加载”和我在这里引用苹果教程: Lazy load images in UITableView

相关问题