MVC模式利用xib檔案定製collectionCell

來源:互聯網
上載者:User

標籤:fit   main   功能   let   tco   nonatomic   錯誤   ble   comment   

資料來源於豆瓣網~僅供學習交流~

本執行個體練慣用到了SDWebImage架構:實現從網路端下載圖片的功能
:https://github.com/rs/SDWebImage

實現效果及架構:

xib檔案:Class是與之相聯絡的檔案

代碼部分:
Modal部分
CollectionModal.h

#import <Foundation/Foundation.h>@interface CollectionModal : NSObject@property (nonatomic,retain) NSDictionary *images;@property (nonatomic,copy) NSString *title;@end

CollectionModal.m 能夠什麼都不寫,也能夠重寫description來調試用~

View部分
collectionView.h

#import <UIKit/UIKit.h>#import "CollectionModal.h"@interface CollectionCell : UICollectionViewCell//與xib檔案相關聯@property (weak, nonatomic) IBOutlet UIImageView *movieImage;@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//資料@property (nonatomic,retain) CollectionModal *modal;@end

collectionView.m

#import "CollectionCell.h"#import "UIImageView+WebCache.h"@implementation CollectionCell- (void)awakeFromNib {    // Initialization code}- (void)setModal:(CollectionModal *)modal {    _modal = modal;    [self setNeedsLayout];}//布局,當modal賦值以後,調用此函數- (void)layoutSubviews {        [super layoutSubviews];//不要忘了父類方法,不然非常easy出亂七八糟的錯誤    _nameLabel.text = _modal.title;    NSString *str = _modal.images[@"medium"];    [_movieImage sd_setImageWithURL:[NSURL URLWithString:str]];}@end

Controller部分
collectionViewController.h

#import <UIKit/UIKit.h>@interface CollectionViewController : UIViewController<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>//不要忘了遵循協議{    UICollectionView *_collectionView;    NSMutableArray *_modalArray;}@end
#import "CollectionViewController.h"#import "CollectionModal.h"#import "CollectionCell.h"#define Zwidth [UIScreen mainScreen].bounds.size.width#define Zheight [UIScreen mainScreen].bounds.size.height@interface CollectionViewController ()@end@implementation CollectionViewController- (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];    //注冊cell    UINib *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;}

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    CollectionViewController *vc = [[CollectionViewController alloc] init];    self.window.rootViewController = vc;    // Override point for customization after application launch.    return YES;}

關於資料有想要的。能夠評論或私信~哈哈~

MVC模式利用xib檔案定製collectionCell

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.