python tableview没有数据显示_iOS-DZNEmptyDataSet 应用于没有数据的tableView

DZNEmptyDataSet主要应用于没有数据时的tableView,或者其他的scrollview

当用户正在请求数据,或者没有数据时,显示一个空白的页面很不友好,用户可能会一脸懵逼的不知道接下来要干什么,这时候DZNEmptyDataSet作用就出来了,给一张图片提示或者文字提示,用户的好感会大大提升滴~

第一步:安装,只有一个分类

pod 'DZNEmptyDataSet'

image.png

第二步:使用

@interface ViewController ()

DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>

_tableView.emptyDataSetSource = self;

_tableView.emptyDataSetDelegate = self;

// 删除cell分割线,不然没有数据时会显示分割线

_tableView.tableFooterView = [UIView new];

第三步:代理方法

只返回一张图片的提示

// 返回空白页的图片

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {

return [UIImage imageNamed:@"empty"];

}

效果如下:

image.png

返回一张图片的提示+文字的提示

// 返回文字的提示

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {

NSString *title = @"数据为空,请稍后再试~";

NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:20],

NSForegroundColorAttributeName: [UIColor redColor]

};

NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];

return attributedStr;

}

效果如下:

image.png

返回一张图片的提示+文字的提示+描述

// 返回文字的描述

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {

NSString *title = @"哈哈,服务器宕机了,卸载算了^_^";

NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14],

NSForegroundColorAttributeName: [UIColor lightGrayColor]

};

NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];

return attributedStr;

}

效果如下:

image.png

返回一张图片的提示+文字的提示+描述+按钮

// 按钮的文字

- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{

NSString *title = @"我是一个按钮";

NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:14],

NSForegroundColorAttributeName: [UIColor redColor]

};

NSAttributedString *attributedStr = [[NSAttributedString alloc]initWithString:title attributes:attributes];

return attributedStr;

}

// 默认的按钮点击事件

- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {

NSLog(@"默认的按钮点击");

}

效果如下:

image.png

按钮设置图片

// 按钮设置图片

- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{

return [UIImage imageNamed:@"empty"];

}

按钮设置背景图片

// 按钮设置背景图片

- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{

return [UIImage imageNamed:@"empty"];

}

设置颜色

// 设置图片的颜色,设置了就没有图片了,就是一个色块。。。。

- (UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView{

return [UIColor redColor];

}

// 设置空白页面的背景色

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{

return [UIColor greenColor];

}

自定义视图

// 自定义显示视图

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{

UIView *customView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 120)];

UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(50, 0, self.view.frame.size.width-100, 44)];

button.backgroundColor=[UIColor redColor];

[button setTitle:@"我是按钮" forState:UIControlStateNormal];

button.layer.cornerRadius = 10;

button.layer.masksToBounds = YES;

[button addTarget:self action:@selector(sliding) forControlEvents:UIControlEventTouchUpInside];

[customView addSubview:button];

UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake((self.view.frame.size.width-50)/2, 50, 50, 50)];

imageView.image=[UIImage imageNamed:@"empty"];

[customView addSubview:imageView];

UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 20)];

label.text=@"这是自定义视图";

label.textAlignment=NSTextAlignmentCenter;

[customView addSubview:label];

return customView;

}

- (void)sliding {

NSLog(@"sliding");

}

效果如下

image.png

但是自定义的视图获取不到点击方法,所以没办法交互,不晓得为什么。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值