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

 更新时间:2016年11月20日 08:38:50   作者:挨踢的苹果  
最近工作中遇到一个需求,当UITableView数据为空的时候,给出一个简单的提示页面,通过从网上查找解决的方法,发现了两种实现的方法,现在分享给大家,有需要的朋友们可以参考借鉴,下面感兴趣的朋友们来一起学习学习吧。

前言

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

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

#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

具体实现

#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

#import <UIKit/UIKit.h>

@interface UITableView (WFEmpty)

@property (nonatomic, strong, readonly) UIView *emptyView;

-(void)addEmptyViewWithImageName:(NSString*)imageName title:(NSString*)title;

@end

具体实现

#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

总结

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

相关文章

  • iOS实现代码只执行一次

    iOS实现代码只执行一次

    本文给大家分享的是在iOS中控制代码在整个软件生命周期中只运行一次的代码,有需要的小伙伴可以参考下。
    2016-03-03
  • iOS 获取当前时间及时间戳的互换实例

    iOS 获取当前时间及时间戳的互换实例

    下面小编就为大家分享一篇iOS 获取当前时间及时间戳的互换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • iOS自定义View实现卡片滑动

    iOS自定义View实现卡片滑动

    这篇文章主要为大家详细介绍了ios自定义View实现卡片滑动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • iOS UITextField 显示银行卡格式的方法

    iOS UITextField 显示银行卡格式的方法

    下面小编就为大家分享一篇iOS UITextField 显示银行卡格式的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-01-01
  • UIImage加载图片Images.xcassets加载方法的影响

    UIImage加载图片Images.xcassets加载方法的影响

    这篇文章主要介绍了UIImage加载图片Images.xcassets加载方法的影响的相关资料,需要的朋友可以参考下
    2016-12-12
  • iOS实现简易抽屉效果、双边抽屉效果

    iOS实现简易抽屉效果、双边抽屉效果

    这篇文章主要为大家详细介绍了两款iOS抽屉效果,简易抽屉效果、以及双边抽屉效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-02-02
  • iOS开发之TableView实现完整的分割线详解

    iOS开发之TableView实现完整的分割线详解

    在iOS开发中, tableView是我们最常用的UI控件之一。所以这篇文章主要给大家详细介绍了关于iOS中的TableView分割线,有需要的朋友们可以参考借鉴,下面来一起看看吧。
    2016-12-12
  • iOS开发中使app获取本机通讯录的实现代码实例

    iOS开发中使app获取本机通讯录的实现代码实例

    这篇文章主要介绍了iOS开发中使app获取本机通讯录的实现代码实例,主要用到了AddressBook.framework和AddressBookUI.framework,代码基于传统的Objective-C,需要的朋友可以参考下
    2016-01-01
  • iOS实现自定义购物车角标显示购物数量(添加商品时角标抖动 Vie)

    iOS实现自定义购物车角标显示购物数量(添加商品时角标抖动 Vie)

    本文主要介绍了iOS实现自定义购物车及角标显示购物数量(添加商品时角标抖动 Vie)的相关知识。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-04-04
  • ios学习笔记之基础数据类型的转换

    ios学习笔记之基础数据类型的转换

    在编码过程中,数据的处理是必要的。众多数据中,NSString、NSData、NSArray、 NSDictionary等数据类型是常用的,对付它们容易,但是在多个数据类型之间转换就需要技巧了。本文主要给大家介绍ios中基础数据类型的转换,有需要的下面来一起看看吧。
    2016-11-11

最新评论