2010-08-11 82 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/