DZNEmptyDataSet用 ListView 的empty處理

android開發的時候,ListView 的empty處理駕輕就熟,覺得iOS開發 UITableView 處理empty 一樣簡單,事實證實,有了GitHub上的這篇文章,證實個人想法非常沒錯的。

Github地址 :https://github.com/dzenbot/DZNEmptyDataSet

Github對這個功能有點喝集成已經說的很清楚了


我作個簡單翻譯

2,優勢

Empty Data Sets are helpful for: android

  • 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.
  • 能夠更好的展示出你的品牌


2,先看效果在決定用不用

Screenshots_row1.png




3,集成
http://cocoadocs.org/docsets/DZNEmptyDataSet/1.6.1
1,從下載的demo中拷貝這 兩個原文件到你的項目裏

UIScrollView+EmptyDataSet.h git

UIScrollView+EmptyDataSet.m github


2,須要顯示empty的table作以下處理 app

@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];
}


好累,不翻譯了 spa

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: code

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{ return [UIImage imageNamed:@"empty_placeholder"];
}

The attributed string for the title of the empty state: ip

- (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: ci

- (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.

相關文章
相關標籤/搜索