iOS空数据处理

作者: Hither | 来源:发表于2016-10-07 21:57 被阅读492次

    本来应该很早就写的 , 一直觉得没有写这个的必要,今天比较闲 想了想还是写下这个。大多数情况下,我们的app 会出现没有数据的时候,这个时候我们又不能让用户在哪里对着一片空白,所以我们需要做点事情;

    .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];
    }
    //没有数据的时候 放一张图片占位
    - (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;
    }
    //点击当前数据空白处时  可以刷新
    - (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
    {
        if (_leftTableView == scrollView) {
            //开始刷新
            NSLog(@"开始刷新");
        }
    }
    //没有数据的时候  tabbleView等  是否可以滚动
    - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
    {
        return YES;
    }
    //没有数据的时候添加文字提示
    - (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

    相关文章

      网友评论

        本文标题:iOS空数据处理

        本文链接:https://www.haomeiwen.com/subject/fuzyyttx.html