简单有效的为UITableView和collectionView添加数据为空的提示
给UITableView添加一个如下分类,在数据源方法里面调用。
UITableView+EmptyData.h文件
#import <UIKit/UIKit.h>
@interface UITableView (EmptyData)
//添加图片和文字
- (void)tableViewDisplayWitMsg:(NSString *)message andImage:(NSString *) image forRowCount:(NSUInteger) rowCount;
//添加文字
- (void)tableViewDisplayWitMsg:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount;
@end
UITableView+EmptyData.m文件
#import "UITableView+EmptyData.h"
@implementation UITableView (EmptyData)
- (void)tableViewDisplayWitMsg:(NSString *)message andImage:(NSString *)image forRowCount:(NSUInteger)rowCount
{
if (rowCount == 0) {
UIView *backView = [[UIView alloc]initWithFrame:self.backgroundView.frame];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:image];
[imageView sizeToFit];
[backView addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(backView);
make.centerY.equalTo(backView).offset(-80);
}];
UILabel *messageLabel = [UILabel new];
messageLabel.text = message;
messageLabel.numberOfLines = 0;
messageLabel.font = [UIFont systemFontOfSize:14];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
[backView addSubview:messageLabel];
[messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imageView.mas_bottom).offset(10);
make.centerX.equalTo(backView);
}];
self.backgroundView = backView;
} else {
self.backgroundView = nil;
}
}
- (void)tableViewDisplayWitMsg:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount
{
if (rowCount == 0) {
// Display a message when the table is empty
// 没有数据的时候,UILabel的显示样式
UILabel *messageLabel = [UILabel new];
messageLabel.text = message;
messageLabel.font = [UIFont systemFontOfSize:24];
// messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.backgroundView = messageLabel;
// self.separatorStyle = UITableViewCellSeparatorStyleNone;
} else {
self.backgroundView = nil;
// self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
@end
给UICollectionView添加一个如下分类,在数据源方法里面调用。
UICollectionView+EmptyData.h文件
#import <UIKit/UIKit.h>
@interface UICollectionView (EmptyData)
//添加图片和文字
- (void)collectionViewDisplayWitMsg:(NSString *)message andImage:(NSString *) image forRowCount:(NSUInteger) rowCount;
//添加文字
- (void)collectionViewDisplayWitMsg:(NSString *) message ifNecessaryForRowCount:(NSUInteger) rowCount;
@end
UICollectionView+EmptyData.m文件
#import "UICollectionView+EmptyData.h"
@implementation UICollectionView (EmptyData)
- (void)collectionViewDisplayWitMsg:(NSString *)message andImage:(NSString *)image forRowCount:(NSUInteger)rowCount
{
if (rowCount == 0) {
UIView *backView = [[UIView alloc]initWithFrame:self.backgroundView.frame];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:image];
[imageView sizeToFit];
[backView addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(backView);
make.centerY.equalTo(backView).offset(-80);
}];
UILabel *messageLabel = [UILabel new];
messageLabel.text = message;
messageLabel.numberOfLines = 0;
messageLabel.font = [UIFont systemFontOfSize:14];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
[backView addSubview:messageLabel];
[messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(imageView.mas_bottom).offset(10);
make.centerX.equalTo(backView);
}];
self.backgroundView = backView;
} else {
self.backgroundView = nil;
}
}
- (void)collectionViewDisplayWitMsg:(NSString *)message ifNecessaryForRowCount:(NSUInteger)rowCount
{
if (rowCount == 0) {
//Display a message when the table is empty
//没有数据的时候,UILabel的显示样式
UILabel *messageLabel = [UILabel new];
messageLabel.text = message;
messageLabel.font = [UIFont systemFontOfSize:24];
//messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.backgroundView = messageLabel;
//self.separatorStyle = UITableViewCellSeparatorStyleNone;
} else {
self.backgroundView = nil;
//self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
网友评论