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

2025-05-24 07:25:36

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:- (NSInteg髫潋啜缅er)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下的- (nul盟敢势袂lable NSAttributedString *)tit造婷用痃leForEmptyDataSet:(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下的- (nul盟敢势袂lable 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如何无嵌入设置空数据显示
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢