iOS mjrefresh 使用

 



 
 


 
 

TableViewController.h

//
//  TableViewContoller.h
//  IteyeBlog
//
//  Created by youbao on 16/10/15.
//  Copyright © 2016年 youbao. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface TableViewContoller : UIViewController





@end

TableViewController.m

//
//  TableViewContoller.m
//  IteyeBlog
//
//  Created by youbao on 16/10/15.
//  Copyright © 2016年 youbao. All rights reserved.
//

#import "TableViewContoller.h"
#import "TableViewCell.h"
#import   "UIImageView+WebCache.h"
#import "OfferEntity.h"
#import "MJRefresh.h"



static NSString *const IconUrl = @"http://ods5pg0qp.bkt.clouddn.com/iteyeblog/icon.png";


@interface TableViewContoller ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, retain)   NSMutableArray  *mDataArray;


@end


@implementation TableViewContoller


#pragma mark - init
#pragma mark   初始化数据
- (NSArray *)mDataArray{
    
    if (!_mDataArray) {
        OfferEntity *entity = [[OfferEntity alloc] init];
        entity.iconurl = IconUrl;
        entity.name = @"baoyou";
        entity.desc = @"IT 工程师";
        
        self.mDataArray = [NSMutableArray array];
        for (int i = 0; i<5; i++) {
            [self.mDataArray addObject:entity];
        }
    }
    return _mDataArray;
}

#pragma mark - 视图管理
#pragma mark   viewdidload
-(void) viewDidLoad{

    [super viewDidLoad];

    NSLog(@"==========view did load=============");
    
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self onRresh];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onLoadMore)];
}



#pragma mark - request
#pragma mark    请求数据
static int currentPage = 0;
static int defualtPageSize = 15;
-(void) onRequest:(int) currIndex{
    if(currIndex == 0){
    
        OfferEntity *entity3 = [[OfferEntity alloc] init];
        entity3.iconurl = IconUrl;
        entity3.name = @"baoyou3";
        entity3.desc = @"IT 工程师3";
        
        [self.mDataArray insertObject:entity3 atIndex:0];
        
    }else{
    
        
    }
    
    
    [self.tableView reloadData];
    
    [self.tableView.mj_header endRefreshing];
    
    if( [_mDataArray count] % defualtPageSize == 0){
        [self.tableView.mj_footer endRefreshing];
    }else{
        [self.tableView.mj_footer resetNoMoreData] ;
    }

    
    
    
}

- (void) onRresh{
    currentPage = 0;
    
    [self onRequest:currentPage];
}
-(void) onLoadMore{
    currentPage += 1;
    [self onRequest:currentPage];
}




#pragma mark - uitableview method
#pragma mark   返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
#pragma mark  对应的section有多少个元素,也就是多少行
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
    return [self.mDataArray count];
}
#pragma mark  指定的 row 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  100.f;
}

#pragma mark  每行cell内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //NSDictionary *item = [self.mDataArray objectAtIndex:indexPath.row];
    OfferEntity *entity =  self.mDataArray[indexPath.row];
    NSLog(@"=================%@",entity );
    
    static NSString * cellIdentifier = @"tableView";
    TableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil){
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
            cell = (TableViewCell *)[nib objectAtIndex:0];
    }
    
       [cell.iconImageView sd_setImageWithURL:[NSURL URLWithString: entity.iconurl ]  placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
    
     cell.name.text = entity.name;
     cell.desctription.text = entity.desc;
    
    cell.onClick = ^(void){
        NSLog(@"=================");
        NSLog(@"%ld",indexPath.row);
    };
    
    return  cell;
}

#pragma mark  点击行,可以做跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}


@end
//
//  TableViewCell.h
//  IteyeBlog
//
//  Created by youbao on 16/10/15.
//  Copyright © 2016年 youbao. All rights reserved.
//

#import <UIKit/UIKit.h>

typedef void( ^ onClick)();


@interface TableViewCell : UITableViewCell


@property (weak, nonatomic) IBOutlet UIImageView * iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *desctription;



@property (nonatomic, copy)    onClick onClick;
//@property (nonatomic, copy)     void ( ^ onClick) (void);

@end
//
//  TableViewCell.m
//  IteyeBlog
//
//  Created by youbao on 16/10/15.
//  Copyright © 2016年 youbao. All rights reserved.
//

#import "TableViewCell.h"


@interface TableViewCell ()



@end


@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageClick:)];
    [self.iconImageView addGestureRecognizer:singleTap];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)onImageClick:(UIButton *)sender{
if(self.onClick)
    self.onClick();
}



@end

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

猜你喜欢

转载自knight-black-bob.iteye.com/blog/2332256