IOS开发 UITableView empty 简单实用
来源:互联网 发布:诺基亚淘宝官方旗舰店 编辑:程序博客网 时间:2024/06/02 02:27
Empty Data Sets are helpful for:
- Avoiding white-screens and communicating to your users why the screen is empty.
- 避免tableview没数据的时候只显示白屏,用户会感激茫然
- Calling to action (particularly as an onboarding process).
- 可以提高操作、交互性
- Avoiding other interruptive mechanisms like showing error alerts.
- 避免tableview没数据的时候用alert这一类的提示强行打断用户的操作
- Beeing consistent and improving the user experience.
- 一致、连贯行好,并且提升用户体验
- Delivering a brand presence.
- 可以更好的展现出你的品牌
UIScrollView+EmptyDataSet.h
UIScrollView+EmptyDataSet.m
2,需要显示empty的table做如下处理
@interface MainViewController : UITableViewController <DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>- (void)viewDidLoad{ [super viewDidLoad]; self.tableView.emptyDataSetSource = self; self.tableView.emptyDataSetDelegate = self; // A little trick for removing the cell separators self.tableView.tableFooterView = [UIView new];}
好累,不翻译了
Data Source Implementation
Return the content you want to show on the empty state, and take advantage of NSAttributedString features to customise the text appearance.
The image for the empty state:
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"empty_placeholder"];}
The attributed string for the title of the empty state:
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ NSString *text = @"Please Allow Photo Access"; NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0], NSForegroundColorAttributeName: [UIColor darkGrayColor]}; return [[NSAttributedString alloc] initWithString:text attributes:attributes];}
The attributed string for the description of the empty state:
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{ NSString *text = @"This allows you to share photos from your library and save photos to your camera roll."; NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new]; paragraph.lineBreakMode = NSLineBreakByWordWrapping; paragraph.alignment = NSTextAlignmentCenter; NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph}; return [[NSAttributedString alloc] initWithString:text attributes:attributes]; }
The attributed string to be used for the specified button state:
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{ NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:17.0]}; return [[NSAttributedString alloc] initWithString:@"Continue" attributes:attributes];}
or the image to be used for the specified button state:
- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{ return [UIImage imageNamed:@"button_image"];}
The background color for the empty state:
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{ return [UIColor whiteColor];}
If you need a more complex layout, you can return a custom view instead:
- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView{ UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [activityView startAnimating]; return activityView;}
Additionally, you can modify the horizontal and/or vertical alignments (as when using a tableHeaderView):
- (CGPoint)offsetForEmptyDataSet:(UIScrollView *)scrollView{ return CGPointMake(0, -self.tableView.tableHeaderView.frame.size.height/2);}
Delegate Implementation
Return the behaviours you would expect from the empty states, and receive the user events.
Asks to know if the empty state should be rendered and displayed (Default is YES) :
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{ return YES;}
Asks for interaction permission (Default is YES) :
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView{ return YES;}
Asks for scrolling permission (Default is NO) :
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES;}
Notifies when the dataset view was tapped:
- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{ // Do something}
Notifies when the data set call to action button was tapped:
- (void)emptyDataSetDidTapButton:(UIScrollView *)scrollView{ // Do something}
Refresh layout
If you need to refresh the empty state layout, simply call:
[self.tableView reloadData];
or
[self.collectionView reloadData];
depending of which you are using.
Force layout update
You can also call [self.tableView reloadEmptyDataSet]
to invalidate the current empty state layout and trigger a layout update, bypassing -reloadData
. This might be useful if you have a lot of logic on your data source that you want to avoid calling, when not needed. [self.tableView reloadEmptyDataSet]
is the only way to refresh content when using with UIScrollView.
- IOS开发 UITableView empty 简单实用
- iOS开发:UITableView简单介绍
- UITableView的简单实用
- iOS开发:sqlite的简单实用
- iOS UITableView简单用法
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI基础—18UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- iOS开发UI篇—UITableview控件简单介绍
- ioS开发之UI基础--UITableView简单了解(个人笔记)
- iOS开发-------简单通讯录(UITableView和CoreData的应用)
- iOS开发UI篇—UITableview控件简单介绍
- (温故而知新)iOS开发UI篇—UITableview控件简单介绍
- iOS开发>>>UITableView
- Xamarin ios开发 UITableView
- 【IOS开发】UITableView详解
- iOS-开发UITableView-1
- 优秀博文
- 伪选择器
- NYOJ 38 布线问题(最小生成树--kruskal)
- 9.1数组与字符串(六)——若M*N矩阵中某个元素为0,则将其所在的行与列清零
- HDU 5327 Olympiad
- IOS开发 UITableView empty 简单实用
- 步步为营(十六)搜索(二)BFS 广度优先搜索
- mysql数据一致性和副本复制-part2
- Failed to initialize monitor thread:unable to establish loopback connection
- 9.1数组与字符串(七)——检查s2是否为s1旋转而成
- GCC同时使用静态库和动态库链接
- C# Enum,Int,String的互相转换 枚举转换
- android 自定义dialog初探
- NSArray和NSMutableArray