最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)UITableView數(shù)據(jù)為空時(shí)的提示頁(yè)面

 更新時(shí)間:2016年11月20日 08:38:50   作者:挨踢的蘋(píng)果  
最近工作中遇到一個(gè)需求,當(dāng)UITableView數(shù)據(jù)為空的時(shí)候,給出一個(gè)簡(jiǎn)單的提示頁(yè)面,通過(guò)從網(wǎng)上查找解決的方法,發(fā)現(xiàn)了兩種實(shí)現(xiàn)的方法,現(xiàn)在分享給大家,有需要的朋友們可以參考借鑒,下面感興趣的朋友們來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

前言

相信對(duì)于iOS開(kāi)發(fā)者們來(lái)說(shuō),在開(kāi)發(fā)過(guò)程中,經(jīng)常用UITableView,一定會(huì)遇到數(shù)據(jù)為空的情況,這時(shí)需要在空頁(yè)面上放一個(gè)圖片和一行文字提示數(shù)據(jù)為空,下面整理了兩種方法來(lái)實(shí)現(xiàn)這個(gè)功能。

第一個(gè)是繼承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

具體實(shí)現(xiàn)

#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

具體實(shí)現(xiàn)

#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

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流。

相關(guān)文章

  • CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼

    CAMediaTiming ( 時(shí)間協(xié)議)詳解及實(shí)例代碼

    這篇文章主要介紹了CAMediaTiming / 時(shí)間協(xié)議詳解及實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2016-12-12
  • IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解

    IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解

    這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • iOS自定義水平滾動(dòng)條、進(jìn)度條

    iOS自定義水平滾動(dòng)條、進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了iOS自定義水平滾動(dòng)條、進(jìn)度條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Objective-C Json 實(shí)例詳解

    Objective-C Json 實(shí)例詳解

    這篇文章主要介紹了 Objective-C Json 實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握Object-C Json的使用,需要的朋友可以參考下
    2017-10-10
  • iOS自定義日期選擇器

    iOS自定義日期選擇器

    這篇文章主要為大家詳細(xì)介紹了iOS自定義日期選擇器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 在iOS應(yīng)用中使用UIWebView創(chuàng)建簡(jiǎn)單的網(wǎng)頁(yè)瀏覽器界面

    在iOS應(yīng)用中使用UIWebView創(chuàng)建簡(jiǎn)單的網(wǎng)頁(yè)瀏覽器界面

    這篇文章主要介紹了在iOS應(yīng)用中使用UIWebView創(chuàng)建簡(jiǎn)單的網(wǎng)頁(yè)瀏覽器界面的方法,包括動(dòng)態(tài)獲取UIWebView高度的實(shí)現(xiàn),需要的朋友可以參考下
    2016-01-01
  • iOS中sqlite的詳細(xì)用法

    iOS中sqlite的詳細(xì)用法

    在iOS中,也同樣支持sqlite。目前有很多第三方庫(kù),封裝了sqlite操作,比如swift語(yǔ)言寫(xiě)的SQLite.swift,對(duì)sqlite感興趣的小伙伴們可以參考一下
    2016-05-05
  • 使用UITextField限制只可輸入中,英文,數(shù)字的方法

    使用UITextField限制只可輸入中,英文,數(shù)字的方法

    在我們?nèi)粘i_(kāi)發(fā)中經(jīng)常遇到一些情況,要UITextField只能輸入某一種特定的字符.比如大寫(xiě)A-Z或者小寫(xiě)a-z,或者漢字.或者數(shù)字.那么該如何實(shí)現(xiàn)呢,下面通過(guò)這篇文章來(lái)看看吧。
    2016-09-09
  • iOS利用CALayer實(shí)現(xiàn)動(dòng)畫(huà)加載的效果

    iOS利用CALayer實(shí)現(xiàn)動(dòng)畫(huà)加載的效果

    網(wǎng)上關(guān)于動(dòng)畫(huà)加載的效果大多每一個(gè)圓圈都是使用UIView,因?yàn)檫@種容易控制,但是這里用的是CALayer,文中給出了詳細(xì)的實(shí)現(xiàn)示例代碼,相信會(huì)對(duì)大家的學(xué)習(xí)和理解很有幫助,感興趣的朋友們下面來(lái)一起看看吧。
    2016-10-10
  • iOS Crash文件分析方法匯總

    iOS Crash文件分析方法匯總

    今天跟大家一起聊聊iOSCrash文件的幾種分析方法,都是平時(shí)比較常用的,有需要的小伙伴可以參考下
    2017-11-11

最新評(píng)論

榕江县| 宜昌市| 丹东市| 石楼县| 宝坻区| 普兰县| 松滋市| 息烽县| 综艺| 丹江口市| 韩城市| 无棣县| 那曲县| 乐山市| 澄江县| 浑源县| 岳普湖县| 沾益县| 遂溪县| 仙桃市| 百色市| 安徽省| 如皋市| 陇南市| 大渡口区| 彝良县| 义乌市| 松江区| 昭觉县| 建湖县| 边坝县| 黎川县| 基隆市| 池州市| 田林县| 昌吉市| 苏州市| 鹤壁市| 乌兰察布市| 恩平市| 紫金县|