在寫這篇文章共勉前银亲,先感謝一下DZNEmptyDataSet的作者提供這個(gè)庫(kù),本篇文章是基于DZNEmptyDataSet上進(jìn)行封裝的盖矫,如若不允許丽惭,請(qǐng)聯(lián)系本菜鳥,將第一時(shí)間刪除辈双。謝謝责掏。
本菜鳥之所以封裝這個(gè)頁(yè)面,是因?yàn)樵陂_發(fā)過程總辐马,很多頁(yè)面用戶無(wú)操作的情況下拷橘,頁(yè)面是空的、又如果僅用DZNEmptyDataSet這個(gè)喜爷,每個(gè)頁(yè)面都要寫很多冗疮。所以就做簡(jiǎn)單的封裝,滿足項(xiàng)目的需求檩帐。封裝的也許不好术幔,不喜勿噴,多多指教湃密。共勉...
我的項(xiàng)目遇到的情景有以下情況(也是比較常用到的商城類型頁(yè)面吧...)
1诅挑、無(wú)訂單頁(yè)面 2、購(gòu)物車為空 3泛源、無(wú)收藏頁(yè)面
4拔妥、無(wú)關(guān)注頁(yè)面 5、無(wú)流水頁(yè)面 6达箍、網(wǎng)絡(luò)加載失敗
還有很多無(wú)數(shù)據(jù)顯示的頁(yè)面... 那么這些頁(yè)面不作處理的情況下...頁(yè)面空空的別說(shuō)用戶體驗(yàn)差...自己的開發(fā)的過程都很討厭...
下面進(jìn)入這篇文章的正題 . . .
-------------------封裝需求--------------------
1没龙、默認(rèn)空數(shù)據(jù)頁(yè)面為:顯示默認(rèn)圖片+默認(rèn)文字(暫無(wú)數(shù)據(jù))
2、網(wǎng)絡(luò)加載失敗的頁(yè)面:顯示加載失敗的圖片+文字(網(wǎng)絡(luò)出現(xiàn)問題)
3、在1的基礎(chǔ)上修改默認(rèn)圖片以及文字硬纤,比如:無(wú)收藏頁(yè)面解滓,顯示自定義圖片+文字
4、空數(shù)據(jù)頁(yè)面進(jìn)行操作:比如:無(wú)訂單頁(yè)面筝家,顯示自定義圖片+文字+按鈕(去逛逛洼裤,下單按鈕)
以上四種情況基本滿足項(xiàng)目需求了. . . 還是直接貼效果圖. . . 代碼 . . .
//創(chuàng)建UITableView
- (void)createUI
{
self.title = @"我的訂單";
self.xlTableView.delegate = self;
self.xlTableView.dataSource = self;
}
//創(chuàng)建UITableView
- (void)createUI
{
self.title = @"我的訂單";
self.xlTableView.delegate = self;
self.xlTableView.dataSource = self;
self.noDataImgName = @"NoOrderData";
self.noDataTitle = @"親,您沒有訂單任何訂單消息哦溪王!";
}
//創(chuàng)建UITableView
- (void)createUI
{
self.title = @"我的訂單";
self.xlTableView.delegate = self;
self.xlTableView.dataSource = self;
self.noDataImgName = @"NoOrderData";
self.noDataTitle = @"親腮鞍,您沒有訂單任何訂單消息哦!";
self.noDataDetailTitle = @"您購(gòu)買的寶貝將會(huì)呈現(xiàn)在這里";
}
//創(chuàng)建UITableView
- (void)createUI
{
self.title = @"我的訂單";
self.xlTableView.delegate = self;
self.xlTableView.dataSource = self;
self.noDataImgName = @"NoOrderData";
self.noDataTitle = @"親在扰,您沒有訂單任何訂單消息哦缕减!";
self.noDataDetailTitle = @"您購(gòu)買的寶貝將會(huì)呈現(xiàn)在這里";
self.btnTitle = @"馬上逛逛";
//需要修改按鈕背景圖片,可以這樣做
self.btnImgName = @"noOrderImage";
}
-(void)buttonEvent
{
NSLog(@"馬上逛逛");
}
第一步:下載DZNEmptyDataSet芒珠,將DZNEmptyDataSet里面的UIScrollView+EmptyDataSet.h、UIScrollView+EmptyDataSet.m拉到自己的項(xiàng)目中搅裙。(再次感謝該第三方的作者)
第二部:將我封裝的這個(gè)類拉進(jìn)去(直接copy)皱卓,然后創(chuàng)建的新控制器的時(shí)候,繼承這個(gè)類就可以部逮。封裝的這個(gè)類繼承:UIViewController
#import <UIKit/UIKit.h>
@interface XLBaseTableController : UIViewController
@property(nonatomic,retain) UITableView *xlTableView;
//是否顯示空數(shù)據(jù)頁(yè)面 默認(rèn)為顯示
@property(nonatomic,assign) BOOL isShowEmptyData;
//空數(shù)據(jù)頁(yè)面的title -- 可不傳娜汁,默認(rèn)為:暫無(wú)任何數(shù)據(jù)
@property(nonatomic,strong) NSString *noDataTitle;
//空數(shù)據(jù)頁(yè)面的圖片 -- 可不傳,默認(rèn)圖片為:NoData
@property(nonatomic,strong) NSString *noDataImgName;
//顯示副標(biāo)題的時(shí)候兄朋,需要賦值副標(biāo)題掐禁,否則不顯示
@property(nonatomic,strong) NSString *noDataDetailTitle;
//按鈕標(biāo)題、圖片 --不常用
@property(nonatomic,strong) NSString *btnTitle;
@property(nonatomic,strong) NSString *btnImgName;
@end
#import "XLBaseTableController.h"
#import "UIScrollView+EmptyDataSet.h"
@interface XLBaseTableController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@end
@implementation XLBaseTableController
- (void)viewDidLoad {
[super viewDidLoad];
self.isShowEmptyData = YES;
[self.view addSubview:self.myTableView];
self.xlTableView.emptyDataSetSource = self;
self.xlTableView.emptyDataSetDelegate = self;
}
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
if (self.noDataImgName) {
return [UIImage imageNamed:self.noDataImgName];
}
return [UIImage imageNamed:@"NoData"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = self.noDataTitle?self.noDataTitle:@"暫無(wú)任何數(shù)據(jù)";
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
NSForegroundColorAttributeName: [UIColor grayColor]};
return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView
{
NSString *text = self.noDataDetailTitle;
NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
NSForegroundColorAttributeName: [UIColor lightGrayColor],
NSParagraphStyleAttributeName: paragraph};
return self.noDataDetailTitle?[[NSAttributedString alloc] initWithString:text attributes:attributes]:nil;
}
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView
{
return YES;
}
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView
{
return self.isShowEmptyData;
}
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView
{
return -self.xlTableView.tableHeaderView.frame.size.height/2.0f;
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f]};
return self.btnTitle?[[NSAttributedString alloc] initWithString:self.btnTitle attributes:attributes]:nil;
}
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
return self.btnImgName?[UIImage imageNamed:self.btnImgName]:nil;
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
[self buttonEvent];
}
#pragma mark 按鈕事件
-(void)buttonEvent
{
}
-(UITableView *)xlTableView
{
if(_xlTableView == nil)
{
_xlTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height) style:UITableViewStyleGrouped];
_xlTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
_xlTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
_xlTableView.showsVerticalScrollIndicator = NO;
_xlTableView.showsHorizontalScrollIndicator = NO;
}
return _xlTableView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
如果此文對(duì)您作用很大颅和,接受打賞傅事,多多益善 . . . 肯定了我的勞動(dòng)成果 . . .
總結(jié):本封裝滿足了大部分項(xiàng)目的需求,我的項(xiàng)目可以說(shuō)幾乎都是直接繼承這個(gè)類的峡扩〔湓剑互相學(xué)習(xí),這個(gè)類還可以進(jìn)一步進(jìn)行封裝教届,由于時(shí)間關(guān)系响鹃,就先與大家共勉到這里。
因?yàn)槭枪裁愕陌秆担卸际墙?jīng)過重寫的买置,目的就是為了可以讓大家直接使用,不像有的作者强霎,只提供部分代碼忿项,然后你懂的 . . . 但是本菜鳥還是建議大家自己敲一遍代碼,掌握了、學(xué)習(xí)了才是自己的 . . .
由于后期會(huì)將項(xiàng)目中此類的封裝繼續(xù)整理更新上來(lái) . . .
所以倦卖,有興趣與本菜鳥一起共勉的洒擦,請(qǐng)?jiān)u論收藏...
demo下載:EmptyDataHandle
歡迎關(guān)注 . . . stars