我將一些圖像動態加載到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