iOS空數(shù)據(jù)處理

本來應(yīng)該很早就寫的 , 一直覺得沒有寫這個(gè)的必要,今天比較閑 想了想還是寫下這個(gè)。大多數(shù)情況下,我們的app 會(huì)出現(xiàn)沒有數(shù)據(jù)的時(shí)候,這個(gè)時(shí)候我們又不能讓用戶在哪里對(duì)著一片空白,所以我們需要做點(diǎn)事情;

.m文件中:
#import "ViewController.h"
#import "UIScrollView+EmptyDataSet.h"
#import "LeftTableViewCell.h"
#define WIDTH self.view.bounds.size.width
#define HEIGHT self.view.bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>{
    UITableView *_leftTableView;
    NSMutableArray *_leftArray;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     _leftArray = [NSMutableArray arrayWithCapacity:0];
    _leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    _leftTableView.delegate = self;
    _leftTableView.backgroundColor = [UIColor whiteColor];
    _leftTableView.dataSource = self;
    
    _leftTableView.emptyDataSetSource = self;
    _leftTableView.emptyDataSetDelegate = self;
    
    
    
    _leftTableView.tableHeaderView = [UIView new];
    _leftTableView.tableFooterView = [UIView new];
    _leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_leftTableView registerNib:[UINib nibWithNibName:@"LeftTableViewCell" bundle:nil] forCellReuseIdentifier:@"leftMessageID"];
    [_leftTableView registerNib:[UINib nibWithNibName:@"LeftTwoTableViewCell" bundle:nil] forCellReuseIdentifier:@"leftTwoMessageID"];
    _leftTableView.estimatedRowHeight = 44.0f;
    _leftTableView.rowHeight = UITableViewAutomaticDimension;
    [self.view addSubview:_leftTableView];
}
//沒有數(shù)據(jù)的時(shí)候 放一張圖片占位
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"icon_none_data"];
}

- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}
//點(diǎn)擊當(dāng)前數(shù)據(jù)空白處時(shí)  可以刷新
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
{
    if (_leftTableView == scrollView) {
        //開始刷新
        NSLog(@"開始刷新");
    }
}
//沒有數(shù)據(jù)的時(shí)候  tabbleView等  是否可以滾動(dòng)
- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}
//沒有數(shù)據(jù)的時(shí)候添加文字提示
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"這里還什么都沒有";
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

#pragma UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _leftArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == _leftTableView) {
        LeftTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"leftMessageID"];
        if (!cell) {
            cell = (LeftTableViewCell*)[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier: @"leftMessageID"];
        }
        cell.name.text = @"";
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
    return nil;
}

@end

效果就是下面的圖:

===========================================

這里是demo:??

Demo

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容