你的位置:首页 > 软件开发 > 操作系统 > 上拉下拉基类

上拉下拉基类

发布时间:2016-07-28 06:00:07
#import <UIKit/UIKit.h>#import "SVPullToRefresh.h"#import "YQZViewController.h" @interface YQZPullViewController ...

#import <UIKit/UIKit.h>

#import "SVPullToRefresh.h"

#import "YQZViewController.h"

 

@interface YQZPullViewController : YQZViewController

{

    IBOutlet UITableView *contentTableView;

    NSMutableArray *dataSource;

    IBOutlet UIView *emptyDataView;

    IBOutlet UIImageView* emptyImgView;

    IBOutlet UILabel *emptyDataLabel;

    int currentPage;

}

 

@property (nonatomic, strong) IBOutlet UITableView *contentTableView;

@property (nonatomic, strong) IBOutlet UIView *emptyDataView;

@property (nonatomic, strong) IBOutlet UIImageView* emptyImgView;

@property (nonatomic, strong) IBOutlet UILabel *emptyDataLabel;

@property (nonatomic, strong) UISegmentedControl *segmentControl;

//进行中新进展数字标签

@property (nonatomic, strong) UILabel *unFinishedLabel;

//已完成新进展数字标签

@property (nonatomic, strong) UILabel *finishedlabel;

//未读数字标签

@property (nonatomic, strong) UILabel *unReadlabel;

 

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, assign) int currentPage;

@property (nonatomic, assign) int totalPages;

 

- (void)insertRowAtTop:(NSArray *)newDatas;

- (void)insertRowAtBottom:(NSArray *)newDatas;

- (void)upRefresh;

- (void)bottomRefresh;

- (void)addSegmentControl;

- (void)addSegmentControlWithArray:(NSArray *)segmentArray;

- (void)addUnReadControl;

- (void)setSegmentNumValue:(NSInteger)unfinishValue finishValue:(NSInteger)finishValue;

- (void)showToast:(NSString *)text;

- (void)resetUIConstraint;

@end

 

 

#import "YQZPullViewController.h"

#import "SDWebImageManager.h"

 

@interface YQZPullViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

 

@implementation YQZPullViewController

 

@synthesize contentTableView;

@synthesize emptyDataView;

@synthesize emptyImgView;

@synthesize emptyDataLabel;

@synthesize dataSource;

@synthesize currentPage;

@synthesize totalPages;

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.dataSource = [NSMutableArray array];

        self.currentPage = 0;

    }

    return self;

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    self.contentTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];

    self.contentTableView.delegate = self;

    self.contentTableView.dataSource = self;

    self.contentTableView.backgroundColor = kYQZLightGrayColor;

    [self.view addSubview:self.contentTableView];

    

    __weak YQZPullViewController *weakself = self;

 

    [self.contentTableView addPullToRefreshWithActionHandler:^{

        [weakself upRefresh];

    }];

    

    // setup infinite scrolling

    [self.contentTableView addInfiniteScrollingWithActionHandler:^{

        [weakself bottomRefresh];

    }];

    if(emptyDataView == nil)

    {

        emptyDataView = [[UIView alloc]init];

        [self.view addSubview:emptyDataView];

    }

    if (emptyImgView == nil) {

        emptyImgView = [[UIImageView alloc]init];

        [emptyDataView addSubview:emptyImgView];

    }

    if (emptyDataLabel == nil) {

        emptyDataLabel = [[UILabel alloc]init];

        [emptyDataLabel setTextColor:[UIColor lightGrayColor]];

        [emptyDataLabel setFont:[UIFont systemFontOfSize:13]];

        [emptyDataView addSubview:emptyDataLabel];

    }

}

 

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

 

- (void)resetUIConstraint

{

    self.contentTableView.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[contentTableView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentTableView)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[contentTableView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentTableView)]];

    self.contentTableView.backgroundColor = kYQZLightGrayColor;

    self.contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    

    self.emptyDataView.translatesAutoresizingMaskIntoConstraints = NO;

    self.emptyImgView.translatesAutoresizingMaskIntoConstraints = NO;

    self.emptyDataLabel.translatesAutoresizingMaskIntoConstraints = NO;

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[emptyDataView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(emptyDataView)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[emptyDataView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(emptyDataView)]];

    

    [emptyDataView addConstraint:[NSLayoutConstraint constraintWithItem:emptyImgView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyDataView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

    [emptyDataView addConstraint:[NSLayoutConstraint

                              constraintWithItem:emptyImgView

                              attribute:NSLayoutAttributeTop

                              relatedBy:NSLayoutRelationEqual

                              toItem:emptyDataView

                              attribute:NSLayoutAttributeTop

                              multiplier:1.0f

                              constant:82.0f]];

    

    [emptyDataView addConstraint:[NSLayoutConstraint constraintWithItem:emptyDataLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyDataView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]];

    [emptyDataView addConstraint:[NSLayoutConstraint

                              constraintWithItem:emptyDataLabel

                              attribute:NSLayoutAttributeTop

                              relatedBy:NSLayoutRelationEqual

                              toItem:emptyDataView

                              attribute:NSLayoutAttributeTop

                              multiplier:1.0f

                              constant:162.0f]];

}

 

- (void)upRefresh{

    [self.contentTableView setShowsInfiniteScrolling:false];

}

 

- (void)bottomRefresh{

//    [self.contentTableView setShowsPullToRefresh:false];

}

 

- (void)showToast:(NSString *)text

{

    [YQZTools showToast:text inView:self.contentTableView.superview];

}

 

- (void)showToast:(NSString *)text width:(float)width

{

    YQZToast *toast = [[YQZToast alloc] initWithFrame:CGRectMake((self.view.frame.size.width - width)/2, self.view.frame.size.height - 100, width, 30)];

    toast.text = text;

    [toast showInView:self.contentTableView];

}

 

#pragma mark - public function -

//给navigationBar添加segment控件

- (void)addSegmentControl

{

    NSArray *segmentArray = @[@"进行中", @"已完成"];

    UIView *segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];

    self.segmentControl = [[UISegmentedControl alloc] initWithItems:segmentArray];

    self.segmentControl.frame = CGRectMake(0, 0, 130, 30);

    self.segmentControl.selectedSegmentIndex = 0;

    self.segmentControl.tintColor = [UIColor whiteColor];

    self.segmentControl.segmentedControlStyle = UISegmentedControlStylePlain;

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.f],UITextAttributeFont, nil];

    [self.segmentControl setTitleTextAttributes:dic forState:UIControlStateNormal];

    [segmentView addSubview:self.segmentControl];

    self.navigationItem.titleView = segmentView;

    

    //进行中新进展数字标签

    self.unFinishedLabel = [[UILabel alloc] initWithFrame:CGRectMake(-8, 3, 16, 16)];

    self.unFinishedLabel.font = [UIFont systemFontOfSize:10.f];

    self.unFinishedLabel.textColor = [UIColor whiteColor];

    self.unFinishedLabel.backgroundColor = kYQZRowNumberRedColor;

    self.unFinishedLabel.layer.cornerRadius = 8;

    self.unFinishedLabel.textAlignment = NSTextAlignmentCenter;

    self.unFinishedLabel.clipsToBounds = YES;

    self.unFinishedLabel.hidden = YES;

    [segmentView addSubview:self.unFinishedLabel];

    

    //已完成新进展数字标签

    self.finishedlabel = [[UILabel alloc] initWithFrame:CGRectMake(122, 3, 16, 16)];

    self.finishedlabel.font = [UIFont systemFontOfSize:10.f];

    self.finishedlabel.textColor = [UIColor whiteColor];

    self.finishedlabel.backgroundColor = kYQZRowNumberRedColor;

    self.finishedlabel.layer.cornerRadius = 8;

    self.finishedlabel.textAlignment = NSTextAlignmentCenter;

    self.finishedlabel.clipsToBounds = YES;

    self.finishedlabel.hidden = YES;

    [segmentView addSubview:self.finishedlabel];

}

 

- (void)addSegmentControlWithArray:(NSArray *)segmentArray

{

    UIView *segmentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 130, 30)];

    self.segmentControl = [[UISegmentedControl alloc] initWithItems:segmentArray];

    self.segmentControl.frame = CGRectMake(0, 0, 130, 30);

    self.segmentControl.selectedSegmentIndex = 0;

    self.segmentControl.tintColor = [UIColor whiteColor];

    self.segmentControl.segmentedControlStyle = UISegmentedControlStylePlain;

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15.f],UITextAttributeFont, nil];

    [self.segmentControl setTitleTextAttributes:dic forState:UIControlStateNormal];

    [segmentView addSubview:self.segmentControl];

    self.navigationItem.titleView = segmentView;

}

 

- (void)addUnReadControl

{

    UIView *segmentView = [[UIView alloc] initWithFrame:CGRectMake(130, 0, 30, 30)];

    //已完成新进展数字标签

    self.unReadlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 3, 16, 16)];

    self.unReadlabel.font = [UIFont systemFontOfSize:10.f];

    self.unReadlabel.textColor = [UIColor whiteColor];

    self.unReadlabel.backgroundColor = kYQZRowNumberRedColor;

    self.unReadlabel.layer.cornerRadius = 8;

    self.unReadlabel.textAlignment = NSTextAlignmentCenter;

    self.unReadlabel.clipsToBounds = YES;

    self.unReadlabel.hidden = NO;

    [segmentView addSubview:self.unReadlabel];

    

    [self.navigationItem.titleView addSubview:segmentView];

}

 

- (void)setSegmentNumValue:(NSInteger)unfinishValue finishValue:(NSInteger)finishValue

{

    if (unfinishValue > 0) {

        if (unfinishValue > 99) {

            self.unFinishedLabel.frame = CGRectMake(-10, 3, 20, 16);

        }

        self.unFinishedLabel.text = [YQZUtility getDisPlayNumString:unfinishValue];

        self.unFinishedLabel.hidden = NO;

    }

    else

    {

        self.unFinishedLabel.hidden = YES;

    }

    

    if (finishValue > 0) {

        if (finishValue > 99) {

            self.finishedlabel.frame = CGRectMake(120, 3, 20, 16);

        }

        self.finishedlabel.text = [YQZUtility getDisPlayNumString:finishValue];

        self.finishedlabel.hidden = NO;

    }

    else

    {

        self.finishedlabel.hidden = YES;

    }

}

 

#pragma mark -

#pragma mark UITableViewDataSource

 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return self.dataSource.count;

}

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 1;

}

 

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

    static NSString *identifier = @"Cell";

    UITableViewCell *cell = [self.contentTableView dequeueReusableCellWithIdentifier:identifier];

    

    if (cell == nil){

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

    }

 

    cell.textLabel.text = [NSString stringWithFormat:@"%ld,%ld",[indexPath section], [indexPath row]];

    return cell;

}

 

- (void)insertRowAtTop:(NSArray *)newDatas {

    

    int64_t delayInSeconds = 2.0;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [self.contentTableView beginUpdates];

        NSRange range = NSMakeRange(0, [newDatas count]);

        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:range];

        [self.dataSource insertObjects:newDatas atIndexes:indexSet];

        NSMutableArray *indexPaths = [NSMutableArray array];

        for (NSInteger i = 0; i < [newDatas count]; i++) {

            [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];

        }

        [self.contentTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationBottom];

        [self.contentTableView endUpdates];

        

        [self.contentTableView.pullToRefreshView stopAnimating];

    });

  

}

 

 

- (void)insertRowAtBottom:(NSArray *)newDatas {

    

    int64_t delayInSeconds = 2.0;

    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);

    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

        [self.contentTableView beginUpdates];

        NSInteger oldCount = [self.dataSource count];

        [self.dataSource addObjectsFromArray:newDatas];

        

         NSMutableArray *indexPaths = [NSMutableArray array];

        for (NSInteger i = 0; i < [newDatas count]; i++) {

            [indexPaths addObject:[NSIndexPath indexPathForRow:(i + oldCount) inSection:0]];

        }

        

        [self.contentTableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];

        [self.contentTableView endUpdates];

        

        [self.contentTableView.infiniteScrollingView stopAnimating];

    });

}

 

@end

使用方法:

从该类继承后 复写4个方法

#pragma mark--------------------------------------------------------

#pragma mark 请求刷新方法

- (void)loadTopData

{

    currentPage = 1;

    [self loadListData:1];

}

 

- (void)loadBottomData

{

    currentPage = currentPage + 1;

    

    [self loadListData:currentPage];

    

//    [self.contentTableView.infiniteScrollingView stopAnimating];

}

 

- (void)upRefresh

{

    [super upRefresh];

    

    [self loadTopData];

}

 

 

- (void)bottomRefresh

{

    [super bottomRefresh];

    

    if (self.currentPage < self.totalPages)

    {

        [self loadBottomData];

    }

    else

    {

        [self.contentTableView.infiniteScrollingView stopAnimating];

        if (self.totalPages > 0) {

            //无数据提示

        }

    }

}

 


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:上拉下拉基类

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录