2010-08-11 80 views
2

幾乎排序與我的第一個應用程序,只是一個簡單的新聞應用程序,但當我加載到我的iPhone上滾動似乎生澀有人可以看看我的功能,看看我是否做某事錯誤。xcode iphone - 生澀的滾動UITableView CellForRowAtIndexPath

我需要在右側的圖像這就是爲什麼我使用自定義單元格。

感謝 任何幫助

#define DATELABEL_TAG 1 #define MAINLABEL_TAG 2 #define PHOTO_TAG 3 


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

{ 

static NSString *MainNewsCellIdentifier = @"MainNewsCellIdentifier"; 


UILabel *mainLabel, *dateLabel; 

UIImageView *photo; 


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: MainNewsCellIdentifier]; 


    if (cell == nil) 

{ 

     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier: MainNewsCellIdentifier] autorelease]; 

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 


dateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,15.0,170.0,15.0)] autorelease]; 

dateLabel.tag = DATELABEL_TAG; 

dateLabel.font = [UIFont systemFontOfSize:10.0]; 

dateLabel.textAlignment = UITextAlignmentLeft; 

dateLabel.textColor = [UIColor darkGrayColor]; 

dateLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin; //| UIViewAutoresizingFlexibleHeight; 

[cell.contentView addSubview:dateLabel]; 


mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(15.0,28.0,170.0,60.0)] autorelease]; 

mainLabel.tag = MAINLABEL_TAG; 

mainLabel.font = [UIFont boldSystemFontOfSize:14.0]; 

mainLabel.textColor = [UIColor blackColor]; 

mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin; 

mainLabel.numberOfLines = 0; 

//mainLabel.backgroundColor = [UIColor greenColor]; 

[cell.contentView addSubview:mainLabel]; 


photo = [[[UIImageView alloc] initWithFrame:CGRectMake(190.0,15.0,85.0,85.0)] autorelease]; 

photo.tag = PHOTO_TAG; 

photo.contentMode = UIViewContentModeScaleAspectFit;//UIViewContentModeScaleAspectFit; // 


[cell.contentView addSubview:photo]; 


    } 

else { 


dateLabel = (UILabel *)[cell.contentView viewWithTag:DATELABEL_TAG]; 

mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG]; 

photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG]; 

} 


NSUInteger row = [indexPath row]; 

NSDictionary *stream = (NSDictionary *) [dataList objectAtIndex:row]; 

NSString *title = [stream valueForKey:@"title"]; 



NSString *titleString = @""; 


if(! [title isKindOfClass:[NSString class]]) 

{ 

titleString = @""; 

} 

else 

{ 

titleString = title; 

} 


CGSize maximumSize = CGSizeMake(180, 9999); 


    UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14]; 

    CGSize dateStringSize = [titleString sizeWithFont:dateFont 

constrainedToSize:maximumSize 

lineBreakMode:mainLabel.lineBreakMode]; 


    CGRect dateFrame = CGRectMake(15.0, 28.0, 170.0, dateStringSize.height); 

    mainLabel.frame = dateFrame; 


mainLabel.text = titleString; 

dateLabel.text = [stream valueForKey:@"created"]; 


NSString *i = [NSString stringWithFormat:@"http://www.website.co.uk/images/%@", [stream valueForKey:@"image"]]; 

NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]]; 

UIImage *newsImage = [[UIImage alloc] initWithData:imageURL]; 


photo.image = newsImage; 

[imageURL release]; 

[newsImage release]; 


    return cell; 

} 

回答

0

問題是這樣的:

NSString *i = [NSString stringWithFormat:@"http://www.website.co.uk/images/%@", [stream valueForKey:@"image"]]; 

NSData *imageURL = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:i]]; 

UIImage *newsImage = [[UIImage alloc] initWithData:imageURL]; 

您可以有效地在這裏說,那隻要電池需要顯示,圖像必須被獲取並呈現。這會花費一些時間 - 對於良好的用戶體驗來說太多了。

您應該在呈現表格視圖之前或之中獲取圖像並緩存它們,例如,在一個數組中。或者你必須異步處理,這意味着你在後臺加載,而不是等到return cell;,直到圖像被實際下載。這將有點難以正確。

0

即使你異步下載圖片,你仍然會有一個生澀的卷軸。

爲什麼?這是懶惰的圖像解壓縮。 ios會在屏幕上顯示時執行解壓縮。您必須在後臺線程中手動解壓縮圖像。

解壓縮不僅僅意味着實例化UIImage對象。它可能有點複雜。最好的解決方案是下載SDWebImage並使用附帶的圖像解壓縮程序。 SDWebImage將爲您異步下載並執行解壓縮。

要了解有關該問題的更多信息,請參閱:http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/