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

iOS實現(xiàn)UITableView數據為空時的提示頁面

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

前言

相信對于iOS開發(fā)者們來說,在開發(fā)過程中,經常用UITableView,一定會遇到數據為空的情況,這時需要在空頁面上放一個圖片和一行文字提示數據為空,下面整理了兩種方法來實現(xiàn)這個功能。

第一個是繼承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

具體實現(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

具體實現(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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。

相關文章

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

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

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

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

    這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實例詳解的相關資料,需要的朋友可以參考下
    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創(chuàng)建簡單的網頁瀏覽器界面

    在iOS應用中使用UIWebView創(chuàng)建簡單的網頁瀏覽器界面

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

    iOS中sqlite的詳細用法

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

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

    在我們日常開發(fā)中經常遇到一些情況,要UITextField只能輸入某一種特定的字符.比如大寫A-Z或者小寫a-z,或者漢字.或者數字.那么該如何實現(xiàn)呢,下面通過這篇文章來看看吧。
    2016-09-09
  • iOS利用CALayer實現(xiàn)動畫加載的效果

    iOS利用CALayer實現(xiàn)動畫加載的效果

    網上關于動畫加載的效果大多每一個圓圈都是使用UIView,因為這種容易控制,但是這里用的是CALayer,文中給出了詳細的實現(xiàn)示例代碼,相信會對大家的學習和理解很有幫助,感興趣的朋友們下面來一起看看吧。
    2016-10-10
  • iOS Crash文件分析方法匯總

    iOS Crash文件分析方法匯總

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

最新評論

维西| 巴彦淖尔市| 富阳市| 南汇区| 新宾| 大庆市| 禄丰县| 宾阳县| 讷河市| 南雄市| 徐州市| 宁波市| 班戈县| 浮山县| 白城市| 鄂伦春自治旗| 嘉义市| 包头市| 微博| 虹口区| 桦南县| 衡阳县| 同仁县| 子长县| 洛隆县| 河源市| 周口市| 临漳县| 博白县| 右玉县| 舞钢市| 民乐县| 海口市| 太原市| 鲁山县| 屏东县| 汉寿县| 酉阳| 汉寿县| 六枝特区| 桂阳县|