我将一些图像动态加载到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和@马塞洛,阿尔维斯
我会试一试,我想我可以限制项目列表大概为25,然后在底部做一个“加载25个以上”的单元格,以避免再次大量加载 – craigk 2011-03-29 00:10:42
,任何知道我该怎么做,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
@craigk我猜如果你决定沿着这条路走下去,你可能想看看加载图像异步(而不是有更多的用户界面来加载额外的行)。祝你好运:) – RedBlueThing 2011-03-29 00:18:03