UITableView、UICollectionView 无数据占位

本文介绍了如何在iOS应用中使用DZNEmptyDataSet组件来优雅地处理空数据状态。包括如何配置、设置代理方法以及常见问题的解决方案。

源码地址

DZNEmptyDataSet

config

导入头文件:#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h> 继承:DZNEmptyDataSetSource,DZNEmptyDataSetDelegate

    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
复制代码

e.g.:

#pragma mark: - DZNEmptyDataSetSource Methods
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *imageNameStr = @"common_wifi";
    return [UIImage imageNamed: ([NetworkVerification verificationCurrentNetwork] == -1) ? imageNameStr : @"common_no_record"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"暂无列表信息!";
    if (([NetworkVerification verificationCurrentNetwork] == -1)) {
        text = @"网络连接失败,请检查网络设置";
    }
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:13.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @" ";
    if (([NetworkVerification verificationCurrentNetwork] == -1)) {
        text = @"点击刷新";
    }
    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
    paragraph.lineBreakMode = NSLineBreakByWordWrapping;
    paragraph.alignment = NSTextAlignmentCenter;
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f],
                                 NSForegroundColorAttributeName: [UIColor lightGrayColor],
                                 NSParagraphStyleAttributeName: paragraph};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return [Common hexStringToColor:@"#DEDEDE"];
}

- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return 15;
}

#pragma mark: - DZNEmptyDataSetDelegate Methods
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // Do something
    debugLog(@"dosomeThing");
    //[self.tableView.mj_header beginRefreshing];
    [self checkLocationCityName];
}
复制代码

遇到的问题:不显示出来

  • 1.要遵守数据源和代理协议
  • 2.要刷新
  • 3.刷新完毕后,向上偏移了 解决办法
- (void)emptyDataSetWillAppear:(UIScrollView *)scrollView {
    scrollView.contentOffset = CGPointZero;
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值