将动态图像加载到自定义UITableViewCell会减慢滚动速度

时间:2011-03-28 23:19:35

标签: iphone uitableview

我正在将一些图像动态加载到UITableView中并创建一些简单的自定义单元格。

在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并且发现它有点艰难。

如果有人有例子或一些代码可以分享,那将是一个很大的帮助。

非常感谢

更新

选择的答案有效,但在显示UITableView之前加载所有项目。

经过大量剪切和粘贴以及版本控制后,最佳解决方案似乎是MarkJNet's HJCache

这提供了图像缓存以及异步加载,它非常容易实现(虽然我还没有使用它的笔尖,但我希望它不应该太难。

希望能有所帮助。

再次感谢@RedBlueThing和@ Marcelo-Alves

3 个答案:

答案 0 :(得分:0)

您无需覆盖- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 来更改高度,如果不正确,这将真正减慢表格滚动速度。只需在IB中更改它。

答案 1 :(得分: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 之前完成处理。

答案 2 :(得分:0)

经过大量剪切和粘贴以及版本控制后,最佳解决方案似乎是MarkJNet's HJCache

这提供了图像缓存以及异步加载,它非常容易实现(虽然我还没有使用它的笔尖,但我希望它不应该太难。

希望能有所帮助。

再次感谢@RedBlueThing和@ Marcelo-Alves