首页 > 代码库 > 自定义uiview 当没有数据的时候 显示自定义的uiview界面

自定义uiview 当没有数据的时候 显示自定义的uiview界面

////  ZSDTJNoDataView.h//  ZSDTJNoDataView////  Created by Mac on 14-12-28.//  Copyright (c) 2014年 ZSD. All rights reserved.////当发起网络请求的时候没有数据的时候界面显示指定的背景图#import <UIKit/UIKit.h>typedef void(^ZSDNoDataViewCompleteHandler) (NSInteger buttonIndex) ;typedef NS_ENUM(NSInteger, ZSDNoDataViewType){  ZSDNoDataViewTypeInvestMent=0, //投资理财  ZSDNoDataViewTypeRecord,//交易记录  ZSDNoDataViewTypeMyInvest,//我的投资  ZSDNoDataViewTypeNews,//消息中心  ZSDNoDataViewTypePlan //回款计划};@class ZSDTJNoDataView;@protocol ZSDTJNoDataViewDelegate <NSObject>-(void)noDataView:(ZSDTJNoDataView *)noDataView andButtonClick:(NSInteger)selectBtnIndex;@end@interface ZSDTJNoDataView : UIView{    ZSDNoDataViewCompleteHandler completeHandler;}@property(nonatomic,weak)id<ZSDTJNoDataViewDelegate>delegate;+(void)showInBaseView:(UIView *)baseView andNoDataViewType:(ZSDNoDataViewType)noDataViewType andCompeleteHandler:(ZSDNoDataViewCompleteHandler) handler;-(void)showInBaseView:(UIView *)baseView  andNoDataViewDelegate:(id)delegate andNoDataViewType:(ZSDNoDataViewType)noDataViewType;-(void)showInBaseView:(UIView *)baseView andNoDataViewType:(ZSDNoDataViewType)noDataViewType andCompeleteHandler:(ZSDNoDataViewCompleteHandler) handler ;@end////  ZSDTJNoDataView.m//  ZSDTJNoDataView////  Created by Mac on 14-12-28.//  Copyright (c) 2014年 ZSD. All rights reserved.//#import "ZSDTJNoDataView.h"//红色按钮正常状态#define kRedButtonNormalColor [UIColor colorWithRed:0.84 green:0.23 blue:0.29 alpha:1]#define kImageSize CGSizeMake(55.0f,55.0f)@implementation ZSDTJNoDataView{    UIView *contentView;//存放所有控件的view;    UILabel *textLabel;//文本label    UIImageView *photoImageView;//显示图片    UIButton *redButton;//按钮}- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}-(void)setUpNoDataView:(ZSDNoDataViewType)noDataViewType{    //创建一个contentview对象    contentView=[[UIView alloc]init];    contentView.translatesAutoresizingMaskIntoConstraints=NO;    [self addSubview:contentView];        //为contentview添加水平约束    NSArray *contentView_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)];    [self addConstraints:contentView_H];            //创建一个photoImage对象    photoImageView=[[UIImageView alloc]init];    photoImageView.translatesAutoresizingMaskIntoConstraints=NO;    [contentView addSubview:photoImageView];        //为photoImage添加水平约束    NSDictionary *dic_photoImageView=@{@"imageWith":@(kImageSize.width)};    NSArray *photoImageView_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[photoImageView(imageWith)]" options:0 metrics:dic_photoImageView views:NSDictionaryOfVariableBindings(photoImageView)];    //为photoImage添加水平居中约束    NSLayoutConstraint *photoImageView_CX=[NSLayoutConstraint constraintWithItem:photoImageView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:contentView attribute:NSLayoutAttributeCenterX multiplier:1 constant:0];    [contentView addConstraints:photoImageView_H];    [contentView addConstraint:photoImageView_CX];        //创建一个textLabel对象    textLabel=[[UILabel alloc]init];    textLabel.translatesAutoresizingMaskIntoConstraints=NO;    textLabel.backgroundColor = [UIColor clearColor];    textLabel.font = [UIFont systemFontOfSize:17.0f];    textLabel.textAlignment = NSTextAlignmentCenter;    textLabel.textColor = [UIColor colorWithRed:191/255.0f green:191/255.0f blue:191/255.0f alpha:1.0];    [contentView addSubview:textLabel];        //水平添加约束    NSDictionary *dic_textLabel=@{@"LeftMarginRight":@(20.0f)};    NSArray *textLabel_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-LeftMarginRight-[textLabel]-LeftMarginRight-|" options:0 metrics:dic_textLabel views:NSDictionaryOfVariableBindings(textLabel)];    [contentView addConstraints:textLabel_H];        //创建一个redButton对象    redButton=[[UIButton alloc]initWithFrame:CGRectZero];    redButton.tag=0;    redButton.translatesAutoresizingMaskIntoConstraints=NO;    [redButton setTitle:@"马上去投资" forState:UIControlStateNormal];    [redButton setBackgroundColor:kRedButtonNormalColor];    [redButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];    [redButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];    [contentView addSubview:redButton];        //为redButton添加水平约束    NSDictionary *dic_redButton=@{@"LeftorRight":@(16.0f)};    NSArray *redButton_H=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-LeftorRight-[redButton]-LeftorRight-|" options:0 metrics:dic_redButton views:NSDictionaryOfVariableBindings(redButton)];    [contentView addConstraints:redButton_H];        //把所有控件放在最终的父视图中并添加约束    NSDictionary *bindDic=@{@"photoImageView":photoImageView,@"textLabel":textLabel,@"redButton":redButton};    NSDictionary *dic_constraint = @{@"margin":@(15.0f),@"margin_redButton":@(24.0f),@"imgHeight":@(kImageSize.height),@"textHeight":@(20.0f),@"redButtonHeight":@(44.0f)};        NSString *formatStr = [NSString stringWithFormat:@"V:|-[photoImageView(imgHeight)]-margin-[textLabel(textHeight)]-margin_redButton-[redButton(redButtonHeight)]-|"];    NSArray *constraint_V=[NSLayoutConstraint constraintsWithVisualFormat:formatStr options:0 metrics:dic_constraint views:bindDic];    [contentView addConstraints:constraint_V];}-(void)showInView:(UIView *)baseView andNoDataViewType:(ZSDNoDataViewType)noDataViewType{    switch (noDataViewType)    {                    case ZSDNoDataViewTypeInvestMent:        {            photoImageView.image=[UIImage imageNamed:@"invest_circle_none"];            textLabel.text=@"没有相关记录";            break;        }        case ZSDNoDataViewTypeMyInvest:        {            photoImageView.image=[UIImage imageNamed:@"2"];            textLabel.text=@"没有相关记录";            break;        }        case ZSDNoDataViewTypeNews:        {            photoImageView.image=[UIImage imageNamed:@"our_news_none"];            textLabel.text=@"没有相关记录";            break;        }        case ZSDNoDataViewTypePlan:        {            photoImageView.image=[UIImage imageNamed:@"reimbursement_plan_none"];            textLabel.text=@"没有正在回款的项目";            break;        }        case ZSDNoDataViewTypeRecord:        {            photoImageView.image=[UIImage imageNamed:@"Invest_record_none"];            textLabel.text=@"没有相关记录";            break;        }        default:            break;    }    //获取contentview更新约束后实际大小    CGSize contentSize=[contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];    //添加垂直约束    NSDictionary *dic_contentView = @{@"height":@(contentSize.height)};    NSArray *contentView_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[contentView(height)]" options:0 metrics:dic_contentView views:NSDictionaryOfVariableBindings(contentView)];    NSLayoutConstraint *contentView_CY = [NSLayoutConstraint constraintWithItem:contentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];    [self addConstraints:contentView_V];    [self addConstraint:contentView_CY];    [baseView addSubview:self];            self.translatesAutoresizingMaskIntoConstraints = NO;    NSArray *self_H = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[self]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(self)];    NSArray *self_V = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[self]|" options:0 metrics:0 views:NSDictionaryOfVariableBindings(self)];    [baseView addConstraints:self_H];    [baseView addConstraints:self_V];}//代理-(void)showInBaseView:(UIView *)baseView andNoDataViewDelegate:(id)delegate andNoDataViewType:(ZSDNoDataViewType)noDataViewType{    [self setUpNoDataView:noDataViewType];    self.delegate=delegate;    redButton.hidden=noDataViewType==ZSDNoDataViewTypeMyInvest?NO:YES;    [self showInView:baseView andNoDataViewType:noDataViewType];}//block回调-(void)showInBaseView:(UIView *)baseView  andNoDataViewType:(ZSDNoDataViewType)noDataViewType andCompeleteHandler:(ZSDNoDataViewCompleteHandler)handler{    [self setUpNoDataView:noDataViewType];    completeHandler=handler;    redButton.hidden=noDataViewType==ZSDNoDataViewTypeMyInvest?NO:YES;    [self showInView:baseView andNoDataViewType:noDataViewType];    }//类方法block回调+(void)showInBaseView:(UIView *)baseView  andNoDataViewType:(ZSDNoDataViewType)noDataViewType andCompeleteHandler:(ZSDNoDataViewCompleteHandler)handler{    ZSDTJNoDataView *noDataView=[[ZSDTJNoDataView alloc]init];    [noDataView setUpNoDataView:noDataViewType];    noDataView->completeHandler=handler;    noDataView->redButton.hidden=noDataViewType==ZSDNoDataViewTypeMyInvest?NO:YES;    [noDataView showInView:baseView andNoDataViewType:noDataViewType];    }-(void)buttonAction:(UIButton *)sender{    //使用代理    if (_delegate&&[_delegate respondsToSelector:@selector(noDataView:andButtonClick:)])    {        [_delegate noDataView:self andButtonClick:sender.tag];    }    //使用block调    if (completeHandler)    {        completeHandler(sender.tag);    }}@end
#import <UIKit/UIKit.h>#import "ZSDTJNoDataView.h"@interface ZSDViewController : UIViewController@property (weak, nonatomic) IBOutlet UIView *maskView;@end////  ZSDViewController.m//  ZSDTJNoDataView////  Created by Mac on 14-12-28.//  Copyright (c) 2014年 ZSD. All rights reserved.//#import "ZSDViewController.h"@interface ZSDViewController ()<ZSDTJNoDataViewDelegate>{    ZSDTJNoDataView *noDataView;}@end@implementation ZSDViewController- (void)viewDidLoad{    [super viewDidLoad];    noDataView=[[ZSDTJNoDataView alloc]init];    [noDataView showInBaseView:_maskView andNoDataViewType:ZSDNoDataViewTypeMyInvest andCompeleteHandler:^(NSInteger buttonIndex) {               NSLog(@"使用回调时候buttonIndex=%d",buttonIndex);            }];    [noDataView showInBaseView:_maskView andNoDataViewDelegate:self andNoDataViewType:ZSDNoDataViewTypeMyInvest];    [ZSDTJNoDataView showInBaseView:_maskView andNoDataViewType:ZSDNoDataViewTypeMyInvest andCompeleteHandler:^(NSInteger buttonIndex) {                 NSLog(@"使用类方法回调时候buttonIndex=%d",buttonIndex);    }];}- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];}#pragma mark -ZSDTJNoDataView Delegate-(void)noDataView:(ZSDTJNoDataView *)noDataView andButtonClick:(NSInteger)selectBtnIndex{    NSLog(@"使用代理时候selectBtnIndex=%d",selectBtnIndex);}@end

技术分享

自定义uiview 当没有数据的时候 显示自定义的uiview界面