2011-03-28 30 views
1

我将一些图像动态加载到UITableView中并创建一些简单的自定义单元格。将动态图像加载到自定义UITableViewCell会减慢滚动速度

cellForRowAtIndexPath方法中我通过内容通过为每个单元格,像这样:

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

ImagesCell *cell    = (ImagesCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) 
{ 

    NSUInteger row    = [indexPath row]; 
    Playlist *playlist   = [[playlistManager playlists] objectAtIndex:row]; 
    NSString *thumbPath   = [(NSString *) playlist.imagePath stringByAppendingString: @"100x75.png"]; 
    NSArray *nib    = [[NSBundle mainBundle] loadNibNamed:@"ImagesCell" owner:self options:nil]; 

    cell      = [nib objectAtIndex:0]; 
    cell.topLabel.text   = playlist.title; 
    cell.bottomLabel.text  = playlist.artistName; 
    cell.customImageView.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: thumbPath]]]; 
    cell.opaque     = YES; 
    cell.accessoryType   = UITableViewCellAccessoryDetailDisclosureButton; 
} 
return cell;} 

我也改变高度,像这样:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

CGFloat result; 
result = ROW_HEIGHT; 
return result;} 

这一切工作 - 因为它加载/显示内容 - 但是当我在设备上测试它时,它在每个单元格上暂停重绘,造成相当粗糙的滚动。

任何人都可以告诉我如何以解决这个问题的方式加载图像?我看了一些异步例子和苹果LazyLoading示例,但这是我第一次使用iphone proj,并发现它有点艰难。

如果任何人有例子或代码分享,这将是一个很大的帮助。

非常感谢

UPDATE

答案选择的作品,但加载的所有项目中显示的UITableView之前。

经过多次剪切和粘贴和版本控制,最好的解决办法似乎是MarkJNet's HJCache

这使图像缓存以及异步加载,它很容易实现(虽然我还没有得到它与笔尖工作尚未但我希望它不应该太强硬。

希望帮助别人。

再次感谢@RedBlueThing和@马塞洛,阿尔维斯

回答

0

也许你可以在早些时候做图像加载?

尝试添加图像属性的播放列表并做imageWithData当你填充播放目录

更新评论

所以你的播放列表对象头需要包括UIImage对象:

#import <Foundation/Foundation.h> 

@interface Playlist : NSObject 
{ 
    NSURL *imagePath; 
    NSString *title; 
    NSString *subTitle; 
    NSString *mediaType; 
    NSString *artistName; 
    NSInteger id; 

    // Add your image member 
    UIImage * image; 
} 

@property (nonatomic, retain) NSURL *imagePath; 
@property (nonatomic, retain) NSString *title; 
@property (nonatomic, retain) NSString *subTitle; 
@property (nonatomic, retain) NSString *mediaType; 
@property (nonatomic, retain) NSString *artistName; 
@property (nonatomic, readwrite) NSInteger id; 

// and a property , make sure you synthesize this in the implementation 
@property (nonatomic, retain) UIImage * image; 

@end 

当您设置的ImagePath属性,还可以创建UIImage的。

NSString * thumbPath = [(NSString *) self.imagePath stringByAppendingString: @"100x75.png"]; 

// the property is set as retain, so this will increment our reference count 
self.image = [UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString: thumbPath]]]; 

所以,当您设置图像对象(也许在init方法),你这样做,那么将显示的UITableView之前的处理做得很好。

+0

我会试一试,我想我可以限制项目列表大概为25,然后在底部做一个“加载25个以上”的单元格,以避免再次大量加载 – craigk 2011-03-29 00:10:42

+0

,任何知道我该怎么做,Playlist.h如下: #进口<基金会/ Foundation.h> @interface播放列表:NSObject的{ \t NSURL * ImagePath的; \t NSString * title; NSString * subTitle; NSString * mediaType; NSString * artistName; \t NSInteger id; \t } @property(nonatomic,retain)NSURL * imagePath; @property(nonatomic,retain)NSString * title; @property(nonatomic,retain)NSString * subTitle; @property(nonatomic,retain)NSString * mediaType; @property(nonatomic,retain)NSString * artistName; @property(nonatomic,readwrite)NSInteger id; @end – craigk 2011-03-29 00:17:06

+0

@craigk我猜如果你决定沿着这条路走下去,你可能想看看加载图像异步(而不是有更多的用户界面来加载额外的行)。祝你好运:) – RedBlueThing 2011-03-29 00:18:03

0

你并不需要重写- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 来改变高度,如果不正确,这会真的减慢表格的滚动速度。只需在IB中更改即可。

+1

感谢您的答复马塞洛,我已经尝试过这一点,如果一世 当他们从屏幕上滚动然后重新开始时,他们会稍微改变单元格中的位置,将单元格注释掉,单元格会回到默认大小。笔尖与上面的ROW_HEIGHT高度相同。 即使将此注释掉,图像加载也会发生同样的结果。如果我没有图像加载或单元库中的图像相同,则不会出现口吃。 – craigk 2011-03-28 23:55:52

+0

@craigk这是有道理的。从此消息返回ROW_HEIGHT不会影响UITableView的性能。 – RedBlueThing 2011-03-28 23:59:19

+0

只是为了防止有些人在看你的列表中是否有固定的表格单元格高度,只需添加:self.tableView.rowHeight = 80;在你的viewDidLoad方法中,一切都会好起来 – craigk 2011-03-29 07:06:46

0

经过多次剪切和粘贴和版本控制,最好的解决办法似乎是MarkJNet's HJCache

这使图像缓存以及异步加载,它很容易实现(虽然我还没有得到它与笔尖工作尚未但我希望它不应该太强硬。

希望帮助别人。

再次感谢@RedBlueThing和@马塞洛,阿尔维斯

相关问题