2011-03-28 63 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和@馬塞洛,阿爾維斯

相關問題