UICollectionView请求网络数据显示(Text)

TEXT:

AppDelegate.m

    self.window.rootViewController = [[[UINavigationController alloc]initWithRootViewController:[RootViewController new]]autorelease];

RootViewController.m

#import "RootViewController.h"
#import "ImageCell.h"
#import "ImageURL.h"
#define kImageCell @"imagecell"
@interface RootViewController ()<UICollectionViewDataSource>
@property(nonatomic,retain)NSMutableArray *dataSource;//存储model对象
@end
//释放
- (void)dealloc
{
    self.dataSource = nil;
    [super dealloc];
}
//懒加载
- (NSMutableArray *)dataSource{
    
    if (_dataSource == nil) {
        self.dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return [[_dataSource retain]autorelease];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    //调用配置CollectionView
    [self confgureCollectionView];
    //调用解析
    [self readDataFromFile];
    
}
解析数据:

- (void)readDataFromFile{
    //获取文件的路径
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Data.json" ofType:nil];
    //使用文件的初始化NSData对象
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //使用json解析
 NSMutableArray *sourceArray =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers  error:nil];
//    NSLog(@"%@",sourceArray);
    //遍历字典
    for (NSDictionary *dic in sourceArray) {
        //创建model对象
        ImageURL *URL = [[ImageURL alloc]init];
        //添加到model
        [URL setValuesForKeysWithDictionary:dic];
        //添加到数组
        [self.dataSource addObject:URL];
        NSLog(@"%@",self.dataSource);
        
    }
}


配置CollectionView

//配置CollectionView
- (void)confgureCollectionView{
    //创建布局工具
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置item的大小
    flowLayout.itemSize = CGSizeMake(140, 160);
    //设置分区缩进量
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    
    //创建CollectionView对象
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    //配置数据源代理
    collectionView.dataSource = self;
    //注册cell
    [collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:kImageCell];
    //设置背景颜色
    collectionView.backgroundColor = [UIColor whiteColor];  
    
    //添加到父视图上
    [self.view addSubview:collectionView];
    [collectionView release];
    [flowLayout release];
    
}


#pragma mark 数据源代理方法

//返回分区个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.dataSource.count;
    
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kImageCell forIndexPath:indexPath];
    //根据item的下标取出对应位置的数据
    ImageURL *url = self.dataSource[indexPath.item];
    //调用cell控件赋值的方法
    [cell assignValueByImageURL:url];
    
    return cell;
}


自定义cell:

<span style="font-size:24px;">//ImageCell.h
#import <UIKit/UIKit.h>
@class ImageURL;
@interface ImageCell : UICollectionViewCell
//写一个方法给cell上控件赋值
- (void)assignValueByImageURL : (ImageURL *)image;
@end

//ImageCell.m
#import "ImageCell.h"
#import "UIImageView+WebCache.h"
#import "ImageURL.h"
@interface ImageCell ()
@property(nonatomic,retain)UIImageView *photoView;
@end


@implementation ImageCell
- (void)dealloc
{
    self.photoView = nil;
    [super dealloc];
}
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self.contentView addSubview:self.photoView];
    
    }
    return self;
}

- (UIImageView *)photoView{
    
    if (_photoView == nil) {
        self.photoView = [[UIImageView alloc]initWithFrame:self.bounds];
        self.photoView.backgroundColor = [UIColor cyanColor];
        
        
    }
    return [[_photoView retain]autorelease];
    
}
//写一个方法给cell上控件赋值
- (void)assignValueByImageURL : (ImageURL *)image{
    //1.使用图片异步加载
    [self.photoView sd_setImageWithURL:[NSURL URLWithString:image.thumbURL] placeholderImage:[UIImage imageNamed:@"占位1"]];
}
@end</span>

建一个model数据类:
<span style="font-size:24px;">//ImageURL.h
@interface ImageURL : NSObject
@property(nonatomic,copy)NSString *thumbURL;
@end

//ImageURL.m
#import "ImageURL.h"

@implementation ImageURL
- (void)dealloc
{
    self.thumbURL= nil;
    [super dealloc];
}

//防止Crash
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end</span>


效果展示:




图片占位:



------------------------------------------------------------------

Data文件下载:http://pan.baidu.com/s/1ntw5W3f

本节知识点:http://blog.csdn.net/qq_31810357/article/details/49154985

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

韩俊强

奖励一杯咖啡☕️

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值