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

总结

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

相关文章

  • CAMediaTiming ( 时间协议)详解及实例代码

    CAMediaTiming ( 时间协议)详解及实例代码

    这篇文章主要介绍了CAMediaTiming / 时间协议详解及实例代码的相关资料,这里附有实例代码,帮助大家学习参考,需要的朋友可以参考下
    2016-12-12
  • IOS 创建并发线程的实例详解

    IOS 创建并发线程的实例详解

    这篇文章主要介绍了IOS 创建并发线程的实例详解的相关资料,需要的朋友可以参考下
    2017-07-07
  • iOS自定义水平滚动条、进度条

    iOS自定义水平滚动条、进度条

    这篇文章主要为大家详细介绍了iOS自定义水平滚动条、进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • Objective-C Json 实例详解

    Objective-C Json 实例详解

    这篇文章主要介绍了 Objective-C Json 实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握Object-C Json的使用,需要的朋友可以参考下
    2017-10-10
  • iOS自定义日期选择器

    iOS自定义日期选择器

    这篇文章主要为大家详细介绍了iOS自定义日期选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • 在iOS应用中使用UIWebView创建简单的网页浏览器界面

    在iOS应用中使用UIWebView创建简单的网页浏览器界面

    这篇文章主要介绍了在iOS应用中使用UIWebView创建简单的网页浏览器界面的方法,包括动态获取UIWebView高度的实现,需要的朋友可以参考下
    2016-01-01
  • iOS中sqlite的详细用法

    iOS中sqlite的详细用法

    在iOS中,也同样支持sqlite。目前有很多第三方库,封装了sqlite操作,比如swift语言写的SQLite.swift,对sqlite感兴趣的小伙伴们可以参考一下
    2016-05-05
  • 使用UITextField限制只可输入中,英文,数字的方法

    使用UITextField限制只可输入中,英文,数字的方法

    在我们日常开发中经常遇到一些情况,要UITextField只能输入某一种特定的字符.比如大写A-Z或者小写a-z,或者汉字.或者数字.那么该如何实现呢,下面通过这篇文章来看看吧。
    2016-09-09
  • iOS利用CALayer实现动画加载的效果

    iOS利用CALayer实现动画加载的效果

    网上关于动画加载的效果大多每一个圆圈都是使用UIView,因为这种容易控制,但是这里用的是CALayer,文中给出了详细的实现示例代码,相信会对大家的学习和理解很有帮助,感兴趣的朋友们下面来一起看看吧。
    2016-10-10
  • iOS Crash文件分析方法汇总

    iOS Crash文件分析方法汇总

    今天跟大家一起聊聊iOSCrash文件的几种分析方法,都是平时比较常用的,有需要的小伙伴可以参考下
    2017-11-11

最新评论