2009-05-05 43 views
3

檢查出[返回單元格] ..前的最後行的圖片加載後,滾動速度decreasing..it似乎滾動卡住如何在每個單元格中加載圖像時提高表格視圖中滾動的速度?

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

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
    } 

    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1]; 
    NSString *itemDescription=[[stories objectAtIndex: storyIndex] 
               objectForKey:@"title"]; 

    CGRect aframe = CGRectMake(80, 30, 250, 40); 
    textLabel = [[UILabel alloc] initWithFrame:aframe]; 
     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
    textLabel.numberOfLines=0; 
    textLabel.textColor = [UIColor darkTextColor]; 
    textLabel.backgroundColor = [UIColor whiteColor]; 
    [cell.contentView addSubview:textLabel]; 
    textLabel.text=itemDescription; 

    CGRect frame = CGRectMake(0, 0, 70,80); 
    UIImageView *TopImageView = [[UIImageView alloc] init]; 
    [cell.contentView addSubview:TopImageView]; 
    TopImageView.frame=frame; 

    m_strImage = [m_imglinkArray objectAtIndex:storyIndex]; 

    TopImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:m_strImage]]]; 
    TopImageView.image=TopImage; 

    return cell; 
} 

你們能幫我提高速度滾動?

問候

阿倫

+0

你應該改善格式的你,因爲它無法讀取的問題... – 2009-05-05 06:59:53

回答

0

看來你是從一個url每個表視圖要求得到小區一排時加載。這發生在主應用程序線程(用戶界面線程)中,並且它阻止了用戶界面,使其無法響應。

特別是如果這個URL是你從互聯網加載資源,那麼這個停頓是很大的。

我建議您應該加載在後臺線程中的圖像,並顯示圖像佔位符,直到他們從網上下載。

你可以試試下面的網址提供的解決方案......

http://www.markj.net/iphone-asynchronous-table-image/

2

我相信你可以有兩個問題在這裏。首先是沒有多線程圖像加載的問題,其次是你如何使用UITableViewCell。

每當一個細胞出現在屏幕上的iPhone電話的cellForRowAtIndexPath - 如果你向下滾動,高於一切被卸載,當你再次滾動備份重新加載。正如已經指出的那樣,這可以通過多線程模式解決 - 或者將鏈接發佈到markj.net,和/或使用NSThread。 NSThread的優點是您可以更靈活地處理問題或加載更多的圖像數據。 (我會同時使用)

爲了做到這一點,你有效地需要有一個自定義的UITableViewCell。無論如何,這很好,因爲您可以讓表格單元對其內容負責而不是視圖控制器,並且可以高效地操作單元格格式和內容。這是一個偉大的一個鏈接約UITableCellView:http://blog.atrexis.com/index.cfm/2009/1/6/iPhone--Customize-the-UITableCellView

當你實現的UITableViewCell,盡最大努力把所有addSubView和格式電話中的「如果(細胞==無)」塊,只有如果你不能把它們放在你的自定義initWithFrame中。您會發現iPhone正在堆疊您添加的每個子視圖 - 它不會清除舊視圖。通過將所有背景顏色設置爲「UIColor clearColor」,然後在點擊之間改變文字,您可以在工作中看到這一點 - 您將看到一堆文字在彼此之上。這通常不可見,因爲固體填充背景覆蓋了所有「舊」子視圖。

在結合這兩種方法之前,考慮實施「模型」。你有一個視圖和一個控制器,但是你的所有數據都應該在模型中。所有的圖片網址,內容等都應該像NSMutableArray,自己的自定義對象,或者通過SQLite。一旦你有一個模型,你可以實現你的圖像緩存。通過緩存,所有圖像將在您的應用程序負載之間保留,這將節省電池,帶寬和時間。

我會: 1.把你所有的數據放在某種模型中(NSMutableArray,SQLLite,something) 2。實現自己的自定義的UITableViewCell 3.確保你已經0 addSubView,的cellForRowAtIndexPath 4.使用代表團例子或NSThread的積極塊在後臺加載的所有圖像內init或ALLOC電話 5.添加本地圖片緩存

在關於緩存圖像的Apple論壇中,有一些很好的例子。

2

nessence有很多很好的信息。更多的想法:

你在這裏瘋狂地泄漏。你創建的每個單元格都會泄漏2個UILabels,一個UIImageView和一個UIImage。

如前所述,您不僅會泄漏這些內容,而且還會因爲您使用addSubview將其中一個放在另一個之上而積累在視圖中。

在單元格繪製期間接觸到網絡非常慢,並且阻止了您的用戶界面。如果這些URL是本地的,那麼您可以使用UIImage的+ imageWithContentsOfFile,如果不是,則需要在後臺加載。

我不認爲你需要一個線程在這裏。 NSURLConnection是一種在後臺加載數據而不會產生線程開銷的好方法。

nessence是完全正確的,你需要一個Story的模型類。

您可重用單元配置的基本方法不正確。您不會獲取可重用的單元格,然後向其添加子視圖。所有的子視圖都應該添加到if()塊中以創建單元格。然後,在每一遍中,你只需改變事物的價值。我已經重寫了下面的一些代碼來演示。這仍然是不正確的代碼,因爲它在單元格繪製期間觸及網絡,這可能是太多元素在子視圖單元格中(而不是自定義單元格),但它更接近正確的想法。我甚至不知道這是否編譯;我只是在這裏輸入。

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

    static NSString *MyIdentifier = @"MyIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 

    if (cell == nil) { 
     // Here we do all our cell creation 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 

     // Make the label 
     CGRect aframe = CGRectMake(80, 30, 250, 40); 
     UILabel *textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; // Note the -autorelease so we don't leak 

     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     textLabel.numberOfLines=0; 
     textLabel.textColor = [UIColor darkTextColor]; 
     textLabel.backgroundColor = [UIColor whiteColor]; 
     textLabel.tag = DescriptionTag; // A tag so we can find it later (you'll need a constant for this) 
     [cell.contentView addSubview:textLabel]; 

     // And the second label 
     aframe = CGRectMake(80, 30, 250, 40); 
     textLabel = [[[UILabel alloc] initWithFrame:aframe] autorelease]; 
     textLabel.font = [UIFont boldSystemFontOfSize:14]; 
     textLabel.numberOfLines=0; 
     textLabel.textColor = [UIColor darkTextColor]; 
     textLabel.backgroundColor = [UIColor whiteColor]; 
     textLabel.tag = TitleTag; 
     [cell.contentView addSubview:textLabel]; 

     // The image view 
     CGRect frame = CGRectMake(0, 0, 70,80); 
     UIImageView *topImageView = [[[UIImageView alloc] init] autorelease]; 
     topImageView.frame = frame; 
     topImageView.tag = TopImageTag; 
     [cell.contentView addSubview:topImageView]; 
    } 

    // all the above was cell creation; we do that as seldom as possible. 
    // Now we do cell configuration. We want this to be fast. 

    UILabel *descriptionLabel = (UILabel*)[cell.contentView viewWithTag:DescriptionTag]; 
    descriptionLabel.text = itemDescription; 

    UILabel *titleLabel = (UILabel*)[cell.contentView viewWithTag:TitleTag]; 
    titleLabel.text =[[stories objectAtIndex:indexPath.row] objectForKey:@"title"]; 

    NSString *imageURLString = [m_imglinkArray objectAtIndex:storyIndex]; // You should have a model class called Story, not two arrays. 
    UIImage *image = [[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURLString]]] autorelease]; // This is still way too slow if it's a remote URL 
    UIImageView *imageView = (UIImageView*)[cell.contentView viewWithTag:TopImageTag]; 
    imageView.image = image;  

    return cell; 
} 

我建議你花一些時間學習TableViewSuitePractical Memory ManagementCoding Guidelines for Cocoa。一段時間研究Cocoa的基礎知識也會很有用,因爲這裏的編碼風格表明你可能沒有堅實的基礎。雖然這是一本Mac書,但我仍然推薦Cocoa Programming for Mac OS X。如果你有興趣使用這個來學習iPhone,我已經編制了一個syllabus這可能會有所幫助。我還沒有審查過,但斯坦福的在線CS193P course看起來很有希望。

1

這個問題在這裏已經問了好幾次了,你需要做的是異步加載圖像爲了防止滾動速度減慢。

這就是所謂的「懶圖像加載」和我在這裏引用蘋果教程: Lazy load images in UITableView

相關問題