服务器之家

服务器之家 > 正文

iOS实现UITableView数据为空时的提示页面

时间:2021-02-18 16:10     来源/作者:挨踢的苹果

前言

相信对于iOS开发者们来说,在开发过程中,经常用UITableView,一定会遇到数据为空的情况,这时需要在空页面上放一个图片和一行文字提示数据为空,下面整理了两种方法来实现这个功能。

第一个是继承UITableView,在新类中集成图片和文字

?
1
2
3
4
5
6
7
8
9
10
11
#import <UIKit/UIKit.h>
#import "Const.h"
 
@interface WFEmptyTableView : UITableView
 
@property (nonatomic, assign) BOOL showEmptyTipView; // 是否显示背景提示文字
@property (nonatomic, assign) NSInteger vOffset;
@property (nonatomic, copy) NSString *tipString;  // 提示文字
@property (nonatomic, copy) NSString *tipImageName; // 提示图片
 
@end

具体实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#import "WFEmptyTableView.h"
 
@implementation WFEmptyTableView {
 UIView *_customBackView;
 UIImageView *_tipImageView;
 UILabel *_label;
 CGRect _imageFrame;
 CGRect _labelFrame;
 double _scale;
}
 
- (WFEmptyTableView *)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
 self = [super initWithFrame:frame style:style];
 if (self) {
  [self setupViews];
 }
 return self;
}
 
- (void)setupViews {
 _customBackView = [[UIView alloc] initWithFrame:self.frame];
 _customBackView.backgroundColor = [UIColor yellowColor];
 
 _tipImageView = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-200/2)/2, self.frame.size.height/3, 200/2, 200/2)];
 [_customBackView addSubview:_tipImageView];
 _imageFrame = _tipImageView.frame;
 
 _label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_tipImageView.frame), kScreenWidth, 100)];
 
 _label.backgroundColor = [UIColor clearColor];
 _label.textAlignment = NSTextAlignmentCenter;
 _label.textColor = [UIColor lightGrayColor];
 _label.font = [UIFont systemFontOfSize:16];
 _label.lineBreakMode = NSLineBreakByCharWrapping;
 _label.numberOfLines = 0;
 [_customBackView addSubview:_label];
 _labelFrame = _label.frame;
 
}
 
- (void)setShowEmptyTipView:(BOOL)showEmptyTipView {
 _showEmptyTipView = showEmptyTipView;
 if (showEmptyTipView) {
  [self addSubview:_customBackView];
 } else {
  [_customBackView removeFromSuperview];
 }
}
 
- (void)setTipString:(NSString *)tipString {
 _tipString = tipString;
 
 NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:tipString];
 NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
 [paragraphStyle1 setLineSpacing:15];
 [paragraphStyle1 setAlignment:NSTextAlignmentCenter];
 [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [tipString length])];
 [_label setAttributedText:attributedString1];
 
 [self resetFrame];
}
 
- (void)setTipImageName:(NSString *)tipImageName {
 _scale = 1;
 UIImage *image = [UIImage imageNamed:tipImageName];
 _scale = image.size.height*1.0 / image.size.width;
 _tipImageView.image = image;
 
 if (isnan(_scale)) {
  _scale = 1;
 }
 [self resetFrame];
}
 
- (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

?
1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h>
 
@interface UITableView (WFEmpty)
 
@property (nonatomic, strong, readonly) UIView *emptyView;
 
-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title;
 
@end

具体实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#import "UITableView+WFEmpty.h"
#import <objc/runtime.h>
 
static char UITableViewEmptyView;
 
@implementation UITableView (WFEmpty)
 
@dynamic emptyView;
 
- (UIView *)emptyView
{
 return objc_getAssociatedObject(self, &UITableViewEmptyView);
}
 
- (void)setEmptyView:(UIView *)emptyView
{
 [self willChangeValueForKey:@"HJEmptyView"];
 objc_setAssociatedObject(self, &UITableViewEmptyView,
        emptyView,
        OBJC_ASSOCIATION_ASSIGN);
 [self didChangeValueForKey:@"HJEmptyView"];
}
 
 
-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title
{
 if (!self.emptyView)
 {
  CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  UIImage* image = [UIImage imageNamed:imageName];
  NSString* text = title;
 
  UIView* noMessageView = [[UIView alloc] initWithFrame:frame];
  noMessageView.backgroundColor = [UIColor clearColor];
 
  UIImageView *carImageView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-image.size.width)/2, 60, image.size.width, image.size.height)];
  [carImageView setImage:image];
  [noMessageView addSubview:carImageView];
 
  UILabel *noInfoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 160, frame.size.width, 20)];
  noInfoLabel.textAlignment = NSTextAlignmentCenter;
  noInfoLabel.textColor = [UIColor lightGrayColor];
  noInfoLabel.text = text;
  noInfoLabel.backgroundColor = [UIColor clearColor];
  noInfoLabel.font = [UIFont systemFontOfSize:20];
  [noMessageView addSubview:noInfoLabel];
 
  [self addSubview:noMessageView];
 
  self.emptyView = noMessageView;
 }
 
}
 
@end

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

原文链接:http://www.jianshu.com/p/830f9f8416a0

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部