MVC模式利用xib文件定制collectionCell

数据来源于豆瓣网~仅供学习交流~

本实例练习用到了SDWebImage框架:实现从网络端下载图片的功能 下载地址:https://github.com/rs/SDWebImage

实现效果及框架:

xib文件:Class是与之相联系的文件

代码部分: Modal部分 CollectionModal.h

: NSObject@property (nonatomic,retain) NSDictionary *images;@property (nonatomic,copy) NSString *title;@end

CollectionModal.m 可以什么都不写,也可以重写description来调试用~

View部分 collectionView.h

: (weak, nonatomic) IBOutlet UIImageView *movieImage;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//数据@property (nonatomic,retain) CollectionModal *modal;@end

collectionView.m

– (void)awakeFromNib {// Initialization code}- (void)setModal:(CollectionModal *)modal {_modal = modal;[self setNeedsLayout];}//布局,当modal赋值以后,调用此函数- (void)layoutSubviews {[super layoutSubviews];//不要忘了父类方法,,不然很容易出乱七八糟的错误_nameLabel.text = _modal.title;NSString *str = _modal.images[@”medium”];[_movieImage sd_setImageWithURL:[NSURL URLWithString:str]];}@end

Controller部分 collectionViewController.h

: UIViewController<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>//不要忘了遵循协议{UICollectionView *_collectionView;NSMutableArray *_modalArray;}@end()- (void)viewDidLoad {[super viewDidLoad];[self _loadData];[self _creatCollectionView];// Do any additional setup after loading the view.}#pragma mark – Data//文件解析,加载数据- (void)_loadData {NSString *fliePath = [[NSBundle mainBundle] pathForResource:@”exercise” ofType:@”json”];NSData *data = [NSData dataWithContentsOfFile:fliePath];NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];// NSLog(@”%@”,dataDic);_modalArray = [NSMutableArray array];NSArray *subjects = dataDic[@”subjects”];for (NSDictionary *dic in subjects) {CollectionModal *modal = [[CollectionModal alloc] init];modal.images = dic[@”images”];modal.title = dic[@”title”];//NSLog(@”%@”,modal);[_modalArray addObject:modal];//将文件加载到数据数组中}}#pragma mark – collectionView//创建collectionView- (void)_creatCollectionView {UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];layout.minimumInteritemSpacing = 1;//内部cell之间距离layout.minimumLineSpacing = 10;//行间距layout.itemSize = CGSizeMake((Zwidth-4)/3, 200);layout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向设置_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, Zwidth, Zheight) collectionViewLayout:layout];//代理设置_collectionView.dataSource = self;_collectionView.delegate = self;[self.view addSubview:_collectionView];//注册cellUINib *nib = [UINib nibWithNibName:@”CollectionCell” bundle:[NSBundle mainBundle]];[_collectionView registerNib:nib forCellWithReuseIdentifier:@”cell”];}//协议方法的实现,下面两个方法是必须实现的- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {return _modalArray.count;}- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {//这里包含cell的复用思想CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@”cell” forIndexPath:indexPath];CollectionModal *modal = _modalArray[indexPath.row];cell.modal = modal;return cell;}我想一个人旅行,可以不带相机,也不要带上手机,

MVC模式利用xib文件定制collectionCell

相关文章:

你感兴趣的文章:

标签云: