MJ刷新

导框架

pod ‘AFNetworking’
pod ‘MJRefresh’
pod ‘SVProgressHUD’
pod ‘SDWebImage’

ViewController.m

#import "ViewController.h"
#import "TwoViewController.h"
#import <MJRefresh/MJRefresh.h>
#import "MyTableViewCell.h"
@interface ViewController ()<UITableViewDelegate , UITableViewDataSource>
@property(nonatomic , strong)UITableView *table;
@property(nonatomic , strong)NSMutableArray 
*dataSource;
@property(nonatomic , assign)int Page;// 当前页
@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    // 初始化数据
    // 初始化当前数据源
    self.dataSource = [[NSMutableArray alloc]initWithArray:@[@“1”,@“1”,@“1”]];
    // 初始化当前页
    self.Page = 1;

    // 设置导航标题颜色
    self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
    // 设置导航标题
    self.navigationItem.title = @“数码科技”;
    // 修改字体大小
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    // 设置左边按钮图片
    // 创建一个uiview
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(5, 5, 35, 35)];
    // 创建一个uibutton
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 5, 25, 25)];
    [btn setImage:[UIImage imageNamed:@“444”] forState:UIControlStateNormal];
    // 添加到view中
    [view addSubview:btn];
    // 添加到导航视图中
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:view];

    [self.view addSubview:self.table];

    [self addHeaderView];
    [self addHeaderRefresh];

}

// 懒加载
-(UITableView *)table{
    if(!_table){
        
        _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        _table.delegate = self;
        _table.dataSource = self;
        
    }
    [_table registerNib:[UINib nibWithNibName:@"MyTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    return _table;
    
}
-(void)addHeaderView{
    
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    
    // 设置图片
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    // 加载图片
    imgV.image = [UIImage imageNamed:@"111"];
    // 加载到单元格中
    [headerView addSubview:imgV];

    // 设置按钮图片
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 220, 40, 40)];
    [btn setImage:[UIImage imageNamed:@"555"] forState:UIControlStateNormal];
    // 设置点击跳转事件
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:btn];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(45, 265, 100, 10)];
    label.text = @"智能设备";
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:13];
    [headerView addSubview:label];
    
    
    UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(180, 220, 40, 40)];
    [btn1 setImage:[UIImage imageNamed:@"666"] forState:UIControlStateNormal];
    [headerView addSubview:btn1];
    
    UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(175, 265, 100, 10)];
    label1.text = @"游戏装备";
    label1.textColor = [UIColor lightGrayColor];
    label1.font = [UIFont systemFontOfSize:13];
    [headerView addSubview:label1];
    
    
    
    UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(300, 220, 40, 40)];
    [btn2 setImage:[UIImage imageNamed:@"777"] forState:UIControlStateNormal];
    [headerView addSubview:btn2];
    
    UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(295, 265, 100, 10)];
    label2.text = @"手机配件";
    label2.textColor = [UIColor lightGrayColor];
    label2.font = [UIFont systemFontOfSize:13];
    [headerView addSubview:label2];
    
    self.table.tableHeaderView = headerView;
    
}

// 上拉刷新

-(void)addHeaderRefresh{
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        // 刷新的操作
        self.Page ++;
        // 重新发送网络请求
        [self loadData];
        
    }];
    NSArray *imageArr = @[[UIImage imageNamed:@"common_loading_anne_0"],[UIImage imageNamed:@"common_loading_anne_1"]];
    // 正在刷新状态下的图片
    [header setImages:imageArr forState:MJRefreshStateRefreshing];
    // 刷新完毕后的图片
    [header setImages:@[[UIImage imageNamed:@"common_loading_anne_0"]] forState:MJRefreshStateIdle];
    // 添加文字
    [header setTitle:@"敌军还有30S到达战场" forState:MJRefreshStateRefreshing];
    
    self.table.mj_header = header;
    
}

-(void)loadData{

for (int i =0; i<2; i++) {
    [self.dataSource addObject:@(1)];
}
[self.table reloadData];

sleep(2);
[self.table.mj_header endRefreshing];

// [self.table.mj_footer endRefreshing];
}

// 设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return self.dataSource.count;

}
// 设置单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    MyTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(void)click{

TwoViewController *two = [TwoViewController new];

[self.navigationController pushViewController:two animated:YES];

}

猜你喜欢

转载自blog.csdn.net/weixin_43364994/article/details/83719334
MJ
今日推荐