xcode iphone - 生涩的滚动UITableView CellForRowAtIndexPath

时间:2010-08-11 09:51:55

标签: xcode

几乎与我的第一个应用程序排序,只是一个简单的新闻应用程序,但当我将它加载到我的iPhone时,滚动似乎生涩,有人可以看看我的功能,看看我做错了什么。

我需要右侧的图像,这就是我使用自定义单元格的原因。

由于 任何帮助

    #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;

}

2 个答案:

答案 0 :(得分: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;直到实际下载图像。这样做会有点困难。

答案 1 :(得分:0)

即使您异步下载图像,您仍然会有一个不稳定的滚动。

为什么呢?它是懒惰的图像解压缩。 ios会在屏幕上显示时执行解压缩。您必须在后台线程中手动解压缩图像。

解压缩不仅仅意味着实例化UIImage对象。它可能有点复杂。最好的解决方案是下载SDWebImage并使用包含的图像解压缩程序。 SDWebImage将异步下载并为您执行解压缩。

要详细了解该问题,请参阅:http://www.cocoanetics.com/2011/10/avoiding-image-decompression-sickness/