【发布时间】:2016-01-25 12:23:16
【问题描述】:
我的 tableviewcell 的图像出现闪烁的问题。我知道导致问题的原因,只是不确定解决问题的最佳方法。
我正在使用 Parse。
闪烁是由使用缓存策略 CacheThenNetwork 引起的,因此查询会先检查缓存,然后再检查网络是否有任何变化。这会导致查询运行多次,因此 cellForRowAtIndexPath 会被多次调用。
在 cellForRowAtIndexPath 中,我将图像设置为 nil 以避免在单元格出列时出现重复图像。这是闪烁发生的主要原因之一,如果不将其设置为 nil,则每次调用查询时图像都不会消失并再次出现。
所以我的问题是,有没有更好的方法来处理缓存或 cellForRowAtIndexPath 以便我可以避免这种明显的闪烁?
我需要使用这个特定的缓存策略,因为它最适合我正在做的事情。我还需要避免重复图像,因此将图像设置为 nil 对我有用。如果有更好的方法,请指教!
提前致谢。
下面的代码 sn-ps。
-(void)loadTimeline
{
NSLog(@"Loading timeline");
PFQuery *loadTimeline = [PFQuery queryWithClassName:@"Timeline"];
[loadTimeline whereKey:@"objectId" notEqualTo:@""];
loadTimeline.cachePolicy = kPFCachePolicyCacheThenNetwork;
//[loadTimeline clearCachedResult];
[loadTimeline orderByDescending:@"timestamp"];
[loadTimeline findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
if (!error)
{
_timelineArray = [NSMutableArray new];
// Look through all objects in Timeline table
for (PFObject *object in objects)
{
// For every object in the timeline, check the current user's favourites
for (PFObject *favourite in [[Engine sharedInstance] favouritesArray])
{
if ([[object valueForKey:@"club_objectId"] isEqualToString:[favourite valueForKey:@"club_objectId"]])
{
[_timelineArray addObject:object];
}
}
// If post is a Sporter announcement, add it to the array
if ([[object valueForKey:@"type"] isEqualToString:@"sporter_announcement"])
{
[_timelineArray addObject:object];
}
}
[[self tableView] reloadData];
}
}];
}
在 cellForRowAtIndexPath 中设置图像:
// Club badge image
[cell.clubBadgeButton setImage:nil forState:UIControlStateNormal];
PFFile *badgeImageFile = object[@"badge_image"];
[badgeImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *badgeImage = [UIImage imageWithData:imageData];
[cell.clubBadgeButton setImage:badgeImage forState:UIControlStateNormal];
}
}];
【问题讨论】:
标签: ios xcode caching parse-platform uiimage