【发布时间】:2014-02-03 16:40:23
【问题描述】:
我有一个集合视图控制器,其中包含从 JSON 调用获取其内容的单元格。每个单元格都有一个滚动视图(图像幻灯片)。我注意到幻灯片正在加载在旧幻灯片之上(我可以看到旧单元格中的幻灯片出现在新幻灯片加载之前)。或者,如果我滑动到第一个单元格的第三个图像,然后滚动第四个单元格,它会显示我的第三个图像(而不是第一个),但页面控件会显示这是第一张幻灯片。
如何在生成新单元格时“清除”旧单元格(或至少清除滚动视图,或阻止其重复使用)?
Article.h (NSObject)
@interface Article : NSObject
@property (readonly) NSURL *imageURL;
@property (nonatomic, strong) NSArray *images;
- (instancetype)initWithAttributes:(NSDictionary *)attributes;
+ (void)latestNewsWithBlock:(void (^)(NSArray *news, NSError *error))block;
@end
#pragma mark -
@interface ArticleImage : NSObject
@property (readonly, nonatomic, strong) NSURL *URL;
- (instancetype)initWithAttributes:(NSDictionary *)attributes;
@end
Article.m (NSObject)
@interface Article ()
@property (readwrite, nonatomic, strong) NSURL *URL;
@end
@implementation Article
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
self = [super init];
if (!self) {
return nil;
}
self.URL = [NSURL URLWithString:attributes[@"url"]];
NSMutableArray *mutableImages = [NSMutableArray array];
for (NSDictionary *imageAttributes in attributes[@"photos"]) {
NSDictionary *imageFileAttributes = [imageAttributes valueForKeyPath:@"photo_file.photo_file"];
ArticleImage *image = [[ArticleImage alloc] initWithAttributes:imageFileAttributes];
[mutableImages addObject:image];
}
self.images = mutableImages;
return self;
}
- (NSURL *)imageURL {
return [[self.images firstObject] URL];
}
+ (void)latestNewsWithBlock:(void (^)(NSArray *news, NSError *error))block {
[[DeadstockAPIManager sharedManager] GET:@"articles" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
NSMutableArray *mutableNews = [NSMutableArray array];
for (NSDictionary *attributes in JSON[@"articles"]) {
Article *news = [[Article alloc] initWithAttributes:attributes];
[mutableNews addObject:news];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (block) {
block([NSArray arrayWithArray:mutableNews], nil);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (block) {
block(nil, error);
}
}];
}
@end
#pragma mark -
@interface ArticleImage ()
@property (readwrite, nonatomic, strong) NSURL *URL;
@property (readwrite, nonatomic, strong) NSURL *thumbnailURL;
@end
@implementation ArticleImage
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
self = [super init];
if (!self) {
return nil;
}
self.URL = [NSURL URLWithString:[attributes valueForKeyPath:@"thumb.url"]];
return self;
}
集合视图控制器
- (void)setLatestNews:(NSArray *)latestNews {
_latestNews = latestNews;
[self.collectionView reloadData];
}
- (void)loadData:(id)sender {
[Article latestNewsWithBlock:^(NSArray *news, NSError *error) {
self.latestNews = news;
}];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[ArticleCell class] forCellWithReuseIdentifier:@"ArticleCell"];
[self loadData:nil];
}
#pragma mark - UICollectionViewDataSource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"articleCell";
ArticleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.article = [self.latestNews objectAtIndex:indexPath.row];
return cell;
}
集合视图单元格
@interface ArticleCell ()
@property (readwrite, nonatomic, strong) NSArray *pageImages;
@end
@implementation ArticleCell
- (void)setArticle:(Article *)article {
_article = article;
self.pageImages = [self.article.images valueForKeyPath:@"URL"];
NSUInteger pageCount = [self.pageImages count];
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
self.pageControl.hidden = (pageCount == 1);
for (NSInteger page = 0; page < pageCount; page++) {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
UIImageView *imageView = [[UIImageView alloc] init];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.frame = frame;
[imageView setImageWithURL:self.pageImages[page]];
[self.scrollView addSubview:imageView];
}
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
}
谢谢。
【问题讨论】:
-
顺便说一句,您通常不希望创建像
article属性那样具有副作用的属性。相反,删除setArticle并创建一个您调用的-(void)configureForArticle:(Article *)article方法,然后该方法可以执行所有必要的代码并设置article属性。
标签: ios json uiscrollview afnetworking uicollectionviewcell