Fork me on GitHub

第二十一讲.UICollectionView(集合视图)以及瀑布流效果, 通知中心(NSNotificationCenter).

一.集合视图(UICollectionView)

1.集合视图的概念

 

 

2.如何创建

 

3.集合视图的布局UICollectionViewFlowLayout

 

 

 

4.自定义cell和 布局协议UICollectionViewDelegateFlowLayout

     使用时cell一般选择自定义,而在布局时候要使用代理.

 

 UICollectionView的基本使用示例代码分析:

 

#import "ViewController.h"
#import "CollectionReusableView.h"

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadCollectionView];

    //注册方法创建UICollectionView
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CELL"];
    
 
}

-(void)loadCollectionView
{
    //布局类 控制每一个item的样式
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(100, 100);
    
    //改变上下行的大小(默认是10)
    flowLayout.minimumLineSpacing = 20;
    
    //设置两个item中间的大小(默认为10)
    flowLayout.minimumInteritemSpacing = 37.5;

    //控制举例屏幕的距离  上 左 下 右
    //flowLayout.sectionInset = UIEdgeInsetsMake(100, 100, 100, 0);
    
    
    //横向滑动
    //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    

    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    //背景色
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.view addSubview:self.collectionView];

    
    //注册haeder和footer方法
     //给collectionView 注册一个headerView
    [self.collectionView registerClass:[CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    //给collectionView 注册一个footerView
      [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    
    

}


//返回header和footer  设置header和footer
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{


    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
        return header;
    }  else{
    
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        
        return footer;
    
    
    }
   

}



//设置collectionView的Footer和header的大小-------UICollectionViewDelegateFlowLayout代理方法----------
//设置collectionView的Footer的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{

    return CGSizeMake(100, 100);
}


//设置collectionView的header大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{

 return CGSizeMake(100, 100);

}




//创建UICollectionView时,需要实现的两个代理方法-----------------------------------

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 100;


}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //UICollectionViewCell 没有本身携带的控件,就是一个空白的
    //所以在项目中一般都自定义一个UICollectionViewCell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    //设置背景颜色
    cell.backgroundColor = [UIColor redColor];
    
    return cell;

}

 


 

 

二.通知中心(NSNotificationCenter)

 

 

示例代码:

 

NetWorkHandle.h

#import <Foundation/Foundation.h>
#import "Model.h"

typedef void(^MyBlock)(id object);

@interface NetWorkHandle : NSObject

+ (NetWorkHandle *)sharedDataBaseHandle;

-(NSInteger)numberOfDataArray;

-(Model *)modelAtIndexWithDataArray:(NSInteger)index;


@end

 

 NetWorkHandle.m文件

#import "NetWorkHandle.h"


@interface NetWorkHandle ()


@property (nonatomic, strong) NSMutableArray *dataArray;


@end

@implementation NetWorkHandle






//单例类实现方法
+ (NetWorkHandle *)sharedDataBaseHandle
{
    
    static NetWorkHandle *dataBaseHandle = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        if(dataBaseHandle == nil){
            
            dataBaseHandle = [[NetWorkHandle alloc] init];
            
            //在单例类中调用解析方法
            [dataBaseHandle loadPostSnychronization];
            
            
            
        }
        
        
    });
    
    return dataBaseHandle;
    
}


//post数据解析
- (void)loadPostSnychronization
{
    
    self.dataArray = [NSMutableArray array];
    
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
    NSURL *url = [NSURL URLWithString:str];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        

        if(data != nil){
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSArray *arr = dic[@"events"];
            
            for(NSDictionary *dic1 in arr){
                
                Model *model = [[Model alloc] init];
                
                [model setValuesForKeysWithDictionary:dic1];
                
                [self.dataArray addObject:model];
                
                
            }
   
        //name代表通知的名字,所有注册这个名字的接受通知者,都会接受到这个通知 object 是个双保险  通知中心(广播机制)              发送通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"dataBase" object:nil];
            
            
        }
  
        NSLog(@"%@",self.dataArray);
    }];
    
    
}


-(NSInteger)numberOfDataArray
{
    return self.dataArray.count;
     
}


-(Model *)modelAtIndexWithDataArray:(NSInteger)index
{
    
    return self.dataArray[index];
    
}

@end

 

ViewController.m文件

#import "ViewController.h"
#import "NetWorkHandle.h"
#import "Model.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadTableView];
   
}

-(void)loadTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview: self.tableView];
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    
    //去获取一个叫做dataBase的通知  如果它发出来了 就触发这个方法
    [[NSNotificationCenter defaultCenter]   addObserver:self selector:@selector(notic:) name:@"dataBase" object:nil];

}

//通知中心触发方法
-(void)notic:(NSNotificationCenter *)sender
{
    NSLog(@"jgdsf");
    
    [self.tableView reloadData];


}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     //这里调用的是networkhandle的方法  使用单例调用方法
   return [[NetWorkHandle sharedDataBaseHandle] numberOfDataArray];

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    //这里调用的是networkhandle的方法  先调用类方法 再调用对象方法
    Model *model = [[NetWorkHandle sharedDataBaseHandle] modelAtIndexWithDataArray:indexPath.row];;
    
    cell.textLabel.text = model.title;

    return cell;

}

@end

SecdViewController.m文件
#import "SecdViewController.h"
#import "NetWorkHandle.h"

@interface SecdViewController ()

@end

@implementation SecdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    //去获取一个叫做dataBase的通知  如果它发出来了 就出发这个方法
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notic) name:@"dataBase" object:nil];
   
}

-(void)notic
{
    NSLog(@"121");

}

@end

 

 Model.h文件和Model.m文件(模型)

#import <Foundation/Foundation.h>

@interface Model : NSObject


@property(nonatomic,copy)NSString *title;

@end
#import "Model.h"

@implementation Model


-(void)setValue:(id)value forUndefinedKey:(NSString *)key
{


}

@end

 

 

 

 
 
posted @ 2015-10-09 19:10  DengHuiCheng  阅读(266)  评论(0编辑  收藏  举报