iOS實現(xiàn)UITableView數(shù)據(jù)為空時的提示頁面
前言
相信對于iOS開發(fā)者們來說,在開發(fā)過程中,經(jīng)常用UITableView,一定會遇到數(shù)據(jù)為空的情況,這時需要在空頁面上放一個圖片和一行文字提示數(shù)據(jù)為空,下面整理了兩種方法來實現(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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
相關(guān)文章
UIImage加載圖片Images.xcassets加載方法的影響
這篇文章主要介紹了UIImage加載圖片Images.xcassets加載方法的影響的相關(guān)資料,需要的朋友可以參考下2016-12-12iOS開發(fā)之TableView實現(xiàn)完整的分割線詳解
在iOS開發(fā)中, tableView是我們最常用的UI控件之一。所以這篇文章主要給大家詳細介紹了關(guān)于iOS中的TableView分割線,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12iOS開發(fā)中使app獲取本機通訊錄的實現(xiàn)代碼實例
這篇文章主要介紹了iOS開發(fā)中使app獲取本機通訊錄的實現(xiàn)代碼實例,主要用到了AddressBook.framework和AddressBookUI.framework,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01iOS實現(xiàn)自定義購物車角標顯示購物數(shù)量(添加商品時角標抖動 Vie)
本文主要介紹了iOS實現(xiàn)自定義購物車及角標顯示購物數(shù)量(添加商品時角標抖動 Vie)的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04ios學(xué)習(xí)筆記之基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換
在編碼過程中,數(shù)據(jù)的處理是必要的。眾多數(shù)據(jù)中,NSString、NSData、NSArray、 NSDictionary等數(shù)據(jù)類型是常用的,對付它們?nèi)菀?,但是在多個數(shù)據(jù)類型之間轉(zhuǎn)換就需要技巧了。本文主要給大家介紹ios中基礎(chǔ)數(shù)據(jù)類型的轉(zhuǎn)換,有需要的下面來一起看看吧。2016-11-11