UICollectionView之网络图片解析
1:將SDWebImage文件夾的類庫導(dǎo)入工程,創(chuàng)建一個(gè)模型對(duì)象Model類,并聲明好它的屬性,再創(chuàng)建一個(gè)繼承自UICollectionViewCell的自定義類
2:在自定義cell類中重寫
- (instancetype)initWithFrame:(CGRect)frame {
? ? self = [super initWithFrame:frame];
? ? if (self) {
? ? ? ? self.imageView = [[UIImageView alloc] init];
? ? ? ? self.imageView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
? ? ? ? [self.contentView addSubview:_imageView];
? ? ? ? _imageView.backgroundColor = [UIColor blueColor];
? ? }
? ? return self;
}
?
3:創(chuàng)建一個(gè)增廣視圖的類繼承自?UICollectionReusableView,然后將需要顯示的控件聲明成屬性,重寫初始化方法
- (instancetype)initWithFrame:(CGRect)frame {
? ? self = [super initWithFrame:frame];
? ? if (self) {
? ? ? ? self.headerLabel = [[UILabel alloc] initWithFrame:frame];
? ? ? ? self.headerLabel.textAlignment = NSTextAlignmentCenter;
? ? ? ? self.headerLabel.font = [UIFont systemFontOfSize:40];
? ? ? ? [self addSubview:_headerLabel];
? ? }
? ? return self;
}
4:在viewController的.m文件中開始編寫代碼
1):創(chuàng)建collectionView的注意事項(xiàng):
? ??UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
然后通過layout設(shè)置相關(guān)屬性
? ??UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:layout];
collectionView.dataSource = self;
//cell要顯示內(nèi)容的話必須注冊(cè)顯示的控件類
[collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
? ? ? [collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
2):解析數(shù)據(jù)
- (void)handleData {
NSString *filePath = [ [NSBundle mainBundle] pathForSource:@"Data" ofType:@"json"];
NSData *data = [NSData dataWithContentOfFilePath:filePath];
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil ] ;
_dataArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *dic in array) {
??Model *model = [[Model alloc] init];
? ? ? ? [model setValuesForKeysWithDictionary:dic];
? ? ? ? [_dataArray addObject:model];
}
}
3):實(shí)現(xiàn)datasource的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
? ? return 1;
}
?
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
? ? return [_dataArray count];
}
4):實(shí)現(xiàn)兩個(gè)非常重要的方法
a:cell的使用的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
? ??MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
? ? Model *model = _dataArray[indexPath.row];
NSURL *url = [NSURL URLWithString:model.thumbURL];
[cell.imageView sd_setImageWithURL:url placeholderImaeg:[UIImage imageNamed:@"3.png"]];
return cell;
}
b:增廣視圖使用的方法
- (UICollectionReusableView *)
collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
? ? if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
? ? ? ? MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];//此處的類是MyCollectionReusableView
? ? ? ? view.headerLabel.text = @"網(wǎng)絡(luò)圖片";
? ? ? ? return view;
? ? }
? ? return nil;
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/arenouba/p/5217270.html
總結(jié)
以上是生活随笔為你收集整理的UICollectionView之网络图片解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去韩国旅游带多少现金合适
- 下一篇: 装修设计多少钱啊?