【发布时间】:2014-01-03 03:10:37
【问题描述】:
我有一个UITableView,其中包含大量图像。
每个单元格将有 5 张图片,这些图片是从 Parse.com 随机加载的。
我现在的查询代码在cellForRowAtIndexPath。
这会导致一些问题:
1.) 滚动表格时存在一些滞后/延迟。
2.) 现在由于某种奇怪的原因,图像没有显示在正确的位置。
3.) 每次滚动表格时,图像都会重新加载/更改。
我希望图像在此UIView 上仅“加载”一次,在正确的位置,并且滚动流畅。
处理这个问题的最佳方法是什么?
这是我如何加载图像的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// set up the cell here
// query and display random images from parse.com
PFQuery * query = [PFUser query];
[query whereKey:@"category" equalTo:self.cell.label.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error)
{
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
else
{
for (int i = 0; i < objects.count; i++)
{
self.profilePicObject = [objects objectAtIndex:i];
int imgRandom = arc4random() % [objects count];
self.randomProfilePicObject = [objects objectAtIndex:imgRandom];
self.imageFile = [self.randomProfilePicObject objectForKey:@"imageFile"];
NSURL * imageFileURL = [[NSURL alloc] initWithString:self.imageFile.url];
NSData * imageData = [NSData dataWithContentsOfURL:imageFileURL];
UIImage * aImage = [[UIImage alloc] initWithData:imageData];
self.cell.profilePicOne.image = aImage;
}
}
return self.cell;
}
编辑:
我正在使用 SDWebImage 并且表格现在可以平滑滚动并显示图像。
新问题:
图片加载到错误的单元格中。
我正在查询表并将标签文本与我的 Parse 表中的正确字符串匹配,并使用它来显示正确的图像。
最初,有些图像会加载到正确的单元格中,而有些则不会。
当我滚动时,图像会到处出现。
解决此问题的最佳方法是什么?
这是我设置单元格的方式:
static NSString * cellIdentifier = @"FilterSelectCell";
self.cell = (FilterSelectCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (self.cell == nil)
{
NSArray * nib = [[NSBundle mainBundle] loadNibNamed:@"FilterSelectCell" owner:self options:nil];
self.cell = [nib objectAtIndex:0];
self.cell.profilePicOne.clipsToBounds = YES;
self.cell.profilePicOne.layer.cornerRadius = 20.0;
self.cell.profilePicTwo.clipsToBounds = YES;
self.cell.profilePicTwo.layer.cornerRadius = 20.0;
self.cell.profilePicThree.clipsToBounds = YES;
self.cell.profilePicThree.layer.cornerRadius = 20.0;
self.cell.profilePicFour.clipsToBounds = YES;
self.cell.profilePicFour.layer.cornerRadius = 20.0;
self.cell.profilePicFive.clipsToBounds = YES;
self.cell.profilePicFive.layer.cornerRadius = 20.0;
}
self.cell.label.text = [self.myArray objectAtIndex:indexPath.row];
【问题讨论】:
-
您可以预加载一些随机图像,但您认为在 -viewDidLoad 中会需要许多图像。为什么要推迟到他们需要的时候?此外,您可以(并且几乎必须)使用 NSURLConnection 或 SDWebImage 之类的东西在后台下载图像。
标签: ios objective-c uitableview uiimageview parse-platform