网格跳转+刷新

1.coco继承刷新控件pod ‘MJRefresh’
2.v.h

@property(nonatomic , assign)int *Page;

3.v.m

#import "ViewController.h"
#import "t1ViewController.h"
#import "t2ViewController.h"
#import "t3ViewController.h"
#import <MJRefresh.h>
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UITableView *tab;
@property(nonatomic,strong)NSMutableArray * dataSource;
@end
static NSString *coll=@"112";
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray new];
    self.navigationController.navigationBar.barTintColor=[UIColor blackColor];
    self.navigationItem.title=@"数码科技";
    self.navigationController.navigationBar.titleTextAttributes=@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16]};
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"➕" style:UIBarButtonItemStyleDone target:self action:@selector(rightButton)];
    _tab=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tab.delegate=self;
    _tab.dataSource=self;
    
    [self loadData];
    
    [self.view addSubview:_tab];
    [self addheaderRefresh];

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 2;
    }else{
        return 6;
//        return self.dataSource.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cc=@"11";
    UITableViewCell *cell=[_tab dequeueReusableCellWithIdentifier:cc];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cc];
    }
    if(indexPath.section==0){
        if(indexPath.row==0){
            _tab.rowHeight=150;
            UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
            img.image=[UIImage imageNamed:@"京东头视图"];
            [cell addSubview:img];
           
        
        }else{
            _tab.rowHeight=100;
            //初始化流水布局
            UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
            
            //设置格子的大小
            layout.itemSize = CGSizeMake(100, 100);
            
            //设置最小行间距
            layout.minimumLineSpacing = 1;
            
            //设置最小列间距
            layout.minimumInteritemSpacing = 1;
            //设置i网格
//            layout.sectionInset=UIEdgeInsetsMake(10, 10, 10, 20);
            //创建网格
            
            UICollectionView *colV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100) collectionViewLayout:layout];
            
            colV.backgroundColor = [UIColor whiteColor];
            
            //设置数据源 代理
            colV.delegate = self;
            colV .dataSource = self;
            
            //停止滚动
            colV.scrollEnabled = NO;
            
            
            //注册网格
            [colV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:coll];
            
            [cell addSubview:colV];
           
            
//
        }
    }else{
        
    }
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
//设置cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //重用
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:coll forIndexPath:indexPath];
    

    NSArray *arr1 = @[@"1.jpg",@"3.jpg",@"3.jpg"];
    //arr1 = @[@"1.jpg",@"3.jpg",@"3.jpg"];
    UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    img.image=[UIImage imageNamed:arr1[indexPath.row]];
    
    
    //将图片添加到cell
    [cell addSubview:img];
    return cell;
    
}
//点击网格进行跳转
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        NSLog(@"点击了1!");
        t1ViewController *t1=[t1ViewController new];
        [self presentViewController:t1 animated:YES completion:nil];
    }else if(indexPath.row==1){
        t2ViewController *t2=[t2ViewController new];
        [self presentViewController:t2 animated:YES completion:nil];
    }else{
        t3ViewController *t3=[t3ViewController new];
        [self presentViewController:t3 animated:YES completion:nil];
    }
}
-(void)rightButton{
    
}


// 设置分区头部文字

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
   
    
    UIView *view = [[UIView alloc] init];
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width - 15, 20)];
    lab.font = [UIFont systemFontOfSize:15];
    lab.textAlignment=NSTextAlignmentCenter;
    
    lab.textColor = [UIColor blueColor];
    
    [view addSubview:lab];
    
    if(section==0){
        
    }else{
        lab.text=@"热卖单品";
    }
        
    
    return view;
    
}
- (void)refreshDown{
   
}
//数据源
-(void)loadData{
    
    
    
    for (int i =0; i<6; i++) {
        [self.dataSource addObject:@(1)];
    }
    [self.tab reloadData];
    
    sleep(2);
    [self.tab.mj_header endRefreshing];
    [self.tab.mj_footer endRefreshing];
}
// 上拉加载
-(void)addheaderRefresh{
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        // 页面增加
        self.Page ++;
        // 重新发送网络请求
        [self loadData];
        
    }];
    NSArray * imageArr = @[[UIImage imageNamed:@"loading_teemo_1.png"],[UIImage imageNamed:@"loading_teemo_2.png"]];
    [header setImages:imageArr forState:MJRefreshStateRefreshing];
    self.tab.mj_header = header;
}

@end


猜你喜欢

转载自blog.csdn.net/qq_43361450/article/details/83685283