明杰刷新

#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;
@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 addFooterRefresh];
    [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];
    
    
    [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:^{
        // 刷新的操作
        // (1)重置当前页
        self.Page = 1;
        // (2)清空数据源
        [self.dataSource removeAllObjects];
        // (3)重新发送网络请求
        [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)addFooterRefresh{
    
    MJRefreshAutoGifFooter *footer = [MJRefreshAutoGifFooter footerWithRefreshingBlock:^{
        // 页面增加
        self.Page ++;
        // 重新发送网络请求
        [self loadData];
        
    }];
    NSArray * imageArr = @[[UIImage imageNamed:@"loading_teemo_1"],[UIImage imageNamed:@"loading_teemo_2"]];
    [footer setImages:imageArr forState:MJRefreshStateRefreshing];
    self.table.mj_footer = footer;
}
-(void)loadData{
    
    
    
    for (int i =0; i<6; 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 presentViewController:two animated:YES completion:nil];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}`在这里插入代码片`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值