前言:撸一个自己封装使用的无数据占位图,比较简单,当然适合自己的才是最重要的。
废话不多说,一言不合就扔代码。
命名XXNoDataView,继承UIView;先来看.h文件:
import
@interface XXNoDataView : UIView
-(void)reloadWithPicName:(NSString *)picName AndTitle:(NSString *)titleStr;
@end
再来看看.m文件:
import "XXNoDataView.h"
@interface XXNoDataView ()
{
UIImageView *contentImage;
UILabel *titleLabel;
}
@end
@implementation XXNoDataView
-(instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self createView];
}
return self;
}
-- (void)createView{
contentImage = [[UIImageView alloc]init];
contentImage.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2-60);
contentImage.bounds = CGRectMake(0, 0, 68, 68);
[self addSubview:contentImage];
titleLabel = [[UILabel alloc]init];
titleLabel.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2+10);
titleLabel.font = FONT_OF_SIZE(14);
titleLabel.textColor = RGBACOLOR(188, 188, 188, 1);
[self addSubview:titleLabel];
}
-(void)reloadWithPicName:(NSString *)picName AndTitle:(NSString *)titleStr{
contentImage.image = [UIImage imageNamed:picName];
titleLabel.text = titleStr;
CGSize size = [titleLabel sizeThatFits:CGSizeMake(0, 40)];
titleLabel.bounds = CGRectMake(0, 0, size.width, size.height);
}
如何使用:
网络请求数据的时候,判断dateArr.count是否为零;是的话就在当前view加载NoDataView,否的话加载你请求的数据喽。
举个栗子:
if (!(dateArr.count>0)) {
noDataView = [[XXNoDataView alloc]initWithFrame:myHTableView.frame];
[noDataView reloadWithPicName:@"ic_empty" AndTitle:@"暂时没有任务"];
[weakself.view addSubview:noDataView];
}