神刀安全网

[SolutionSummary] #1: UITableView/UICollectionView with empty data

Use case

- Empty data in UITableView/UICollectionView

- No network connection

- Network connection failure

- Loading data

What does it look like before?

// BaseViewController.h - (void)showBlankViewWithString:(NSString *)attention forView:(UIView *)view; - (void)hideBlankView;  // BaseViewController.m #pragma mark - Blank - (void)showBlankViewWithString:(NSString *)attention forView:(UIView *)view {     if (!_blankView) {         CGFloat x = view.frame.origin.x;         CGFloat width = view.frame.size.width;         CGFloat height = view.frame.size.height;         _blankView = [[UIView alloc] initWithFrame:CGRectMake( x + (width - 175)/2.f, height/3.f, 175, 200)];         _blankView.backgroundColor = [UIColor clearColor]; //        [self.view insertSubview:_blankView aboveSubview:view];         [self.view addSubview:_blankView];          UIImageView *blankImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blank_content"]];         blankImgView.frame = CGRectMake(25, 0, 125, 150);         [_blankView addSubview:blankImgView];          _attentionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(blankImgView.frame) + kTVCVCellInset_X, 175, kMediumFontSize + 2)];         _attentionLabel.numberOfLines = 0;         _attentionLabel.textAlignment = NSTextAlignmentCenter;         _attentionLabel.textColor = kLightGrayColor;         _attentionLabel.font = [UIFont systemFontOfSize:kMediumFontSize];         [_blankView addSubview:_attentionLabel];     }     [self.view bringSubviewToFront:_blankView];     _attentionLabel.text = attention; }  - (void)hideBlankView {     if (_blankView != nil) {         [_blankView removeFromSuperview];         _blankView  = nil;     } } 

And call these methods anywhere and choose the right view like this:

[self showBlankViewWithString:kNetworkFailureKey forView:self.view]; 

or write if-else everywhere like this:

    if (tmpArray.count <= 0) {     self.tableView.hidden = YES;     [self showBlankViewWithString:@"No Data" forView:self.view]; } else{     [self hideBlankView];     self.tableView.hidden = NO;     ...... } 

What does it look like now?

Get your data as usual and just call:

[self reloadListData:EmptyType_EmptyData]; 

or

[self reloadListData:EmptyType_NetworkError]; 

or

[self reloadListData:EmptyType_NoNetwork]; 

And all the annoying issues is done!

The only thing you need to do is inheriting from BaseEmptyViewController and set needsEmptyDataHandling to be YES .

// YourViewController.h @interface YourViewController : BaseEmptyViewController @end  // YourViewController.m     self.needsEmptyDataHandling = YES; 

For more details, please check this .

References

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » [SolutionSummary] #1: UITableView/UICollectionView with empty data

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址