我正在尝试设置三个NSMutableArray
以在UITableView
中使用。
这是我的代码:
for (PFObject *object in objects) {
PFUser *user = (PFUser *) object[@"user"];
[ [user objectForKey:@"image"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error)
{
//Add Comment text
[_commentsArray insertObject:[object objectForKey:@"comment"] atIndex:i];
//Add comment Id
[_commentIDArray insertObject:object.objectId atIndex:i];
//Add user image
[_picsArray insertObject:[UIImage imageWithData:data] atIndex:i ];
if (i == [objects count]-1)
{
[self.tableView reloadData];
}
}
else
{
NSLog(@"Errrror == %ld",(unsigned long)[_picsArray count]);
}
i++;
}];
}
在我订购的PFQuery
中:
[query orderByDescending:@"createdAt"];
但据我所知,第一排的图像很大。所以下载它需要时间。所以它进入第二个循环。尝试下载图像。尺寸很小。下载完成。添加到数组。现在下载第一张图片就完成了。添加到阵列但到第二位。如何管理它以便在订单中逐个添加项目?
答案 0 :(得分:0)
不是下载图像和创建数组来填充tableview,而是必须创建PFObject数组并将其与SDWebImage一起用于异步图像下载,而不会出现任何问题或阻止UI。
答案 1 :(得分:0)
检查一下:
// initially, add place holder
for (int i=0; i<objects.count; i++) {
[_commentsArray addObject:@""];
[_commentIDArray addObject:@""];
[_picsArray addObject:@""];
}
for (PFObject *object in objects) {
PFUser *user = (PFUser *) object[@"user"];
[ [user objectForKey:@"image"] getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error)
{
NSInteger orderIndex = [objects indexOfObject:object];
//Replace Comment text
[_commentsArray replaceObjectAtIndex:[object objectForKey:@"comment"] atIndex:orderIndex];
//Replace comment Id
[_commentIDArray replaceObjectAtIndex:object.objectId atIndex:orderIndex];
//Replace user image
[_picsArray replaceObjectAtIndex:[UIImage imageWithData:data] atIndex:orderIndex ];
if (i == [objects count]-1)
{
[self.tableView reloadData];
}
}
else
{
NSLog(@"Errrror == %ld",(unsigned long)[_picsArray count]);
}
i++;
}];
}
答案 2 :(得分:0)
我猜这个问题实际上是关于在可见行仍然被提取时,不会花费超出滚动位置下载图像的努力。
该问题的解决方案是在cellForRowAtIndexPath:
中配置单元格时需要懒散加载图像。有很多关于这个想法的通用内容。对于特定于解析的解决方案see PFImageView here。
要点是图像视图将负责从文件加载和缓存图像。它会以异步方式执行此操作,因此会出现较低的感知延迟。只需将文件提供给图像视图,然后让它完成其余的工作......
您的cellForRowAtIndexPath
看起来像这样:
// just guessing that your "objects" array is the table's datasource
PFObject *object = self.objects[indexPath.row];
PFUser *user = (PFUser *) object[@"user"];
// put a PFImageView in the cell (in this case with a tag==32)
PFImageView *imageView = (PFImageView *)[cell viewWithTag:32];
imageView.image = [UIImage imageNamed:@”placeholder.png”];
imageView.file = [user objectForKey:@"image"]; // assuming this is a file attribute
[imageView loadInBackground];
答案 3 :(得分:0)
您遇到一个问题,即您尝试执行基于订单的添加,其中您的块以异步方式触发,因此它可以按随机顺序排列。
您应该更改为字典或任何其他键控数据结构,并使用键进行评论和图片(例如,使用注释ID作为键)。
还要仔细检查是否在主队列或任何串行队列上执行了块的回调,因为如果不是,则需要添加锁。
答案 4 :(得分:0)
我遇到了同样的问题,我的图片已经下载但没有按顺序显示,我的桌面视图图片和标题不匹配。
为了解决这个问题,我在Parse.com的课程中创建了一个专门保存nameForImages的列,然后使用此名称保存每个下载的图像。
nameForImages必须与列标题相同,例如:
这个技巧适合解决我的问题,因为图像的名称和出现在单元格中的标题很短,没有特殊的字符。
我希望它能帮助或解决问题,祝你好运。