iOS实现UITableView数据为空时的提示页面
前言
相信对于iOS开发者们来说,在开发过程中,经常用UITableView,一定会遇到数据为空的情况,这时需要在空页面上放一个图片和一行文字提示数据为空,下面整理了两种方法来实现这个功能。
第一个是继承UITableView,在新类中集成图片和文字
#import<UIKit/UIKit.h> #import"Const.h" @interfaceWFEmptyTableView:UITableView @property(nonatomic,assign)BOOLshowEmptyTipView;//是否显示背景提示文字 @property(nonatomic,assign)NSIntegervOffset; @property(nonatomic,copy)NSString*tipString;//提示文字 @property(nonatomic,copy)NSString*tipImageName;//提示图片 @end
具体实现
#import"WFEmptyTableView.h"
@implementationWFEmptyTableView{
UIView*_customBackView;
UIImageView*_tipImageView;
UILabel*_label;
CGRect_imageFrame;
CGRect_labelFrame;
double_scale;
}
-(WFEmptyTableView*)initWithFrame:(CGRect)framestyle:(UITableViewStyle)style{
self=[superinitWithFrame:framestyle:style];
if(self){
[selfsetupViews];
}
returnself;
}
-(void)setupViews{
_customBackView=[[UIViewalloc]initWithFrame:self.frame];
_customBackView.backgroundColor=[UIColoryellowColor];
_tipImageView=[[UIImageViewalloc]initWithFrame:CGRectMake((kScreenWidth-200/2)/2,self.frame.size.height/3,200/2,200/2)];
[_customBackViewaddSubview:_tipImageView];
_imageFrame=_tipImageView.frame;
_label=[[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_tipImageView.frame),kScreenWidth,100)];
_label.backgroundColor=[UIColorclearColor];
_label.textAlignment=NSTextAlignmentCenter;
_label.textColor=[UIColorlightGrayColor];
_label.font=[UIFontsystemFontOfSize:16];
_label.lineBreakMode=NSLineBreakByCharWrapping;
_label.numberOfLines=0;
[_customBackViewaddSubview:_label];
_labelFrame=_label.frame;
}
-(void)setShowEmptyTipView:(BOOL)showEmptyTipView{
_showEmptyTipView=showEmptyTipView;
if(showEmptyTipView){
[selfaddSubview:_customBackView];
}else{
[_customBackViewremoveFromSuperview];
}
}
-(void)setTipString:(NSString*)tipString{
_tipString=tipString;
NSMutableAttributedString*attributedString1=[[NSMutableAttributedStringalloc]initWithString:tipString];
NSMutableParagraphStyle*paragraphStyle1=[[NSMutableParagraphStylealloc]init];
[paragraphStyle1setLineSpacing:15];
[paragraphStyle1setAlignment:NSTextAlignmentCenter];
[attributedString1addAttribute:NSParagraphStyleAttributeNamevalue:paragraphStyle1range:NSMakeRange(0,[tipStringlength])];
[_labelsetAttributedText:attributedString1];
[selfresetFrame];
}
-(void)setTipImageName:(NSString*)tipImageName{
_scale=1;
UIImage*image=[UIImageimageNamed:tipImageName];
_scale=image.size.height*1.0/image.size.width;
_tipImageView.image=image;
if(isnan(_scale)){
_scale=1;
}
[selfresetFrame];
}
-(void)setVOffset:(NSInteger)vOffset{
_label.frame=CGRectMake(CGRectGetMinX(_label.frame),CGRectGetMinY(_label.frame)+vOffset,CGRectGetWidth(_label.frame),CGRectGetHeight(_label.frame));
_tipImageView.frame=CGRectMake(CGRectGetMinX(_tipImageView.frame),CGRectGetMinY(_tipImageView.frame)+vOffset,CGRectGetWidth(_tipImageView.frame),CGRectGetHeight(_tipImageView.frame));
}
-(void)resetFrame{
_tipImageView.frame=CGRectMake(0,CGRectGetMinY(_tipImageView.frame),150,150*_scale);
_tipImageView.center=CGPointMake(kScreenWidth/2.0,_tipImageView.center.y);
_label.frame=CGRectMake(CGRectGetMinX(_label.frame),CGRectGetMaxY(_tipImageView.frame),CGRectGetWidth(_label.frame),CGRectGetHeight(_label.frame));
}
@end
还有一种方法,是用Category
#import<UIKit/UIKit.h> @interfaceUITableView(WFEmpty) @property(nonatomic,strong,readonly)UIView*emptyView; -(void)addEmptyViewWithImageName:(NSString*)imageNametitle:(NSString*)title; @end
具体实现
#import"UITableView+WFEmpty.h"
#import<objc/runtime.h>
staticcharUITableViewEmptyView;
@implementationUITableView(WFEmpty)
@dynamicemptyView;
-(UIView*)emptyView
{
returnobjc_getAssociatedObject(self,&UITableViewEmptyView);
}
-(void)setEmptyView:(UIView*)emptyView
{
[selfwillChangeValueForKey:@"HJEmptyView"];
objc_setAssociatedObject(self,&UITableViewEmptyView,
emptyView,
OBJC_ASSOCIATION_ASSIGN);
[selfdidChangeValueForKey:@"HJEmptyView"];
}
-(void)addEmptyViewWithImageName:(NSString*)imageNametitle:(NSString*)title
{
if(!self.emptyView)
{
CGRectframe=CGRectMake(0,0,self.frame.size.width,self.frame.size.height);
UIImage*image=[UIImageimageNamed:imageName];
NSString*text=title;
UIView*noMessageView=[[UIViewalloc]initWithFrame:frame];
noMessageView.backgroundColor=[UIColorclearColor];
UIImageView*carImageView=[[UIImageViewalloc]initWithFrame:CGRectMake((frame.size.width-image.size.width)/2,60,image.size.width,image.size.height)];
[carImageViewsetImage:image];
[noMessageViewaddSubview:carImageView];
UILabel*noInfoLabel=[[UILabelalloc]initWithFrame:CGRectMake(0,160,frame.size.width,20)];
noInfoLabel.textAlignment=NSTextAlignmentCenter;
noInfoLabel.textColor=[UIColorlightGrayColor];
noInfoLabel.text=text;
noInfoLabel.backgroundColor=[UIColorclearColor];
noInfoLabel.font=[UIFontsystemFontOfSize:20];
[noMessageViewaddSubview:noInfoLabel];
[selfaddSubview:noMessageView];
self.emptyView=noMessageView;
}
}
@end
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。