我在做android开发的时候,ListVIew 有设置EmptyView的功能。
但是在IOS 未发现这个功能,其实很多情况下,这个功能还是比较好用的。
但是想网上找一个,也没找到让人眼睛一亮。所以自己打算写一个。
- 其实一开始我打算用扩展来实现这个功能,这样的话,不需要动老代码就实现了功能。 但是发现,用扩展很难完成这个功能,首先我想要 reload 的时候,判断是否有数据,是否显示emptyView。扩展的话,不好处理reload。
- 那么我想用,KVO的形式来判断 是否有数据,但是发现,也没啥办法能监听到数据的变化。
- 我只能用继承了。
#import <UIKit/UIKit.h>
@interface BaseTableView : UITableView
//Empty View
@property (nonatomic ,strong) UIView *emptyView;
@end
@interface BaseTableView(EmptyView)
-(void)showEmptyView;///<显示
-(void)dismissEmptyView;///<消失
@end
@interface TableEmptyView : UIView
@end
.m
#import "BaseTableView.h"
@implementation BaseTableView
-(void)reloadData{
[super reloadData];
if ([self.visibleCells count] > 0)
[self dismissEmptyView];
else
[self showEmptyView];
}
@end
@implementation BaseTableView(EmptyView)
-(void)showEmptyView{
if (!self.emptyView) {
self.emptyView = [[TableEmptyView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), CGRectGetHeight(self.bounds))];
}
if (![self.emptyView superview]) {
[self addSubview:self.emptyView];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.emptyView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.emptyView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
self.emptyView.hidden = NO;
[self bringSubviewToFront:self.emptyView];
}
-(void)dismissEmptyView{
self.emptyView.hidden = YES;
[self sendSubviewToBack:self.emptyView];
[self.emptyView removeFromSuperview];
}
@end
@implementation TableEmptyView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
UIView *emptyContentView = [[UIView alloc] init];
[emptyContentView setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty.png"]];
[icon setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *lable = [[UILabel alloc] init];
lable.text = @"还没有收到消息哦~";
lable.textColor = [UIColor darkGrayColor];
[lable setTranslatesAutoresizingMaskIntoConstraints:NO];
[emptyContentView addSubview:icon];
[emptyContentView addSubview:lable];
NSString *Vcons = @"V:|-(1)-[icon]-(5)-[lable]-(1)-|";
[emptyContentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:Vcons options:0 metrics:0 views:NSDictionaryOfVariableBindings(emptyContentView,icon,lable)]];
[emptyContentView addConstraint:[NSLayoutConstraint constraintWithItem:icon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyContentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[emptyContentView addConstraint:[NSLayoutConstraint constraintWithItem:lable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:emptyContentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self addSubview:emptyContentView];
[self addConstraint:[NSLayoutConstraint constraintWithItem:emptyContentView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:emptyContentView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
}
return self;
}
@end
这样就显示出来了。
不知道大家还有没有更好的方法。:(