iOS如何无嵌入设置空数据显示

 时间:2026-02-16 08:20:32

1、使用UIScrollView+EmptyDataSet实现列表的空数据展示,将UIScrollView+EmptyDataSet下载之后将整个文件夹拖入到工程里面,文件夹下只有.h与.m两个文件,结构如下:

iOS如何无嵌入设置空数据显示

2、在需要的控制器界面声明一个tableview的属性,同时遵循

UITableView的代理方法,在这里同时也遵循DZNEmptyDataSetSource与DZNEmptyDataSetDelegate代理方法。

iOS如何无嵌入设置空数据显示

3、在viewDidLoad下初始化UITableView添加到视图上,同时设置代理:

self.tableView.delegate = self;

self.tableView.dataSource = self;

self.tableView.emptyDataSetSource = self;

self.tableView.emptyDataSetDelegate = self;

iOS如何无嵌入设置空数据显示

4、实现UITableView的UITableViewDataSource下的两个方法,分别设置列表行数以及对应的Cell:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];

    }

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    cell.backgroundColor = [UIColor colorWithRed:(random()%256)/255.0 green:(random()%256)/255.0 blue:(random()%256)/255.0 alpha:1.0];

    return cell;

}

iOS如何无嵌入设置空数据显示

5、实现DZNEmptyDataSetSource下的- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView代理方法可以设置空数据显示时的提示文字:

- (nullable NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{

    return [[NSAttributedString alloc] initWithString:@"暂无数据" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];

}

iOS如何无嵌入设置空数据显示

6、实现DZNEmptyDataSetSource下的- (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView代理方法可以设置空数据显示时的提示图片:

- (nullable UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{

    return [UIImage imageNamed:@"icon_nodata"];

}

iOS如何无嵌入设置空数据显示

7、实现DZNEmptyDataSetSource下的- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView代理方法可以设置空数据显示时的界面背景颜色:

- (nullable UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{

    return [UIColor colorWithRed:237/255.0 green:237/255.0 blue:237/255.0 alpha:1.0];

}

iOS如何无嵌入设置空数据显示

8、为了查看效果,先将列表的行数设置为10,列表颜色设置为随机色,效果显示如下:

iOS如何无嵌入设置空数据显示

9、将列表的行数设置为0,空数据界面显示效果如下:

iOS如何无嵌入设置空数据显示

  • Doit.im下载安装方法
  • 成语玩命猜第360、361、362关攻略
  • w68那些你不知道的优点 我也是才知道
  • 石器时代8.0慰灵碑任务
  • 税法(十三)土地增值税法
  • 热门搜索
    笔记本怎么重装系统 赋怎么读 运满满怎么样 怎么看星座 树懒怎么读 headache怎么读 苹果汁怎么榨 怎么瘦大腿 中华鲎怎么读 社保卡怎么办