问题描述
我有一个UITableView从服务器下载它的tableviewcells图像。
I have a UITableView which downloads its tableviewcells images from a server.
我观察到表格滚动得很慢。
我认为这可能是由于下载造成的,但我已经意识到下载完成后表格仍然滚动缓慢且图像图标尺寸非常小。
I thought that this might be due to the downloading, but I have realized that the table still scroll slow after download has finished and the image icon size is very less.
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
btnBack.hidden = FALSE;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
///////////////////// Cell other accessories /////////////////////
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
// cell.backgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_1.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
// cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[[UIImage imageNamed:@"list_2.jpg"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0]];
///////////////////// Cell Title /////////////////////
cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:17.0];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.highlightedTextColor = [UIColor blackColor];
}
///////////////////// Cell Title /////////////////////
cell.textLabel.text = [NSString stringWithFormat:@" %@", [test.arrTitle objectAtIndex:indexPath.row]];
///////////////////// Cell Image /////////////////////
NSString *Path;
Path = [NSString stringWithFormat:@"http://%@",[test.arrImages objectAtIndex:indexPath.row]];
NSLog(@"image-->%@",[test.arrImages objectAtIndex:indexPath.row]);
NSString *strImage = Path;
NSURL *url4Image = [NSURL URLWithString:strImage];
NSData *data = [NSData dataWithContentsOfURL:url4Image];
image =[[UIImage alloc] initWithData:data];
cell.imageView.image =image;
[image release];
return cell;
}
推荐答案
你应该使用一个处理延迟加载图片和自定义tableviewcell。
Google for tweetie custom tableviewcell
这应该会让你朝着正确的方向前进。
You should look to use an NSOperationQueue
to handle lazy loading of images and a custom tableviewcell.
Google for tweetie custom tableviewcellThat should set you in the right direction.
Apple有一个用于在tableViews中下载图像的示例项目:
Apple has a sample project for downloading images in tableViews: LazyTableImages
这篇关于UITableview与图像滚动非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!