【iOS 0 行代码系列】之 0 行代码实现TableView,CollectionView无数据占位图与文字

如果你还没有看过下面的文章,可以花点时间看看


>>>>>1.一行代码完成“空TableView占位视图”管理:

原理:

1.获取 Section 的数量

2.获取每一个 Section 当中 Cell 的数量

>>>>>2.UITableView没数据时用户提示如何做?

https://www.jianshu.com/p/e39699b0d134

原理:

基于 [dataSource count];

一行代码实现 

>>>>>3.iOS 0行代码实现 TableView 无数据时展示占位视图

https://www.jianshu.com/p/246b445ec4e3

原理:

1.获取 Section 的数量

2.获取每一个 Section 当中 Cell 的数量



进入正题:


1.原理


UITableView有一个属性:


@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;


UICollectionView有同样的一个属性:


@property (nonatomic, readonly) NSArray<__kindof UICollectionViewCell *> *visibleCells;


都是获取可见的 Cell

可以根据 reload 之后,可见 Cell 的数量来判断,列表是否为空。



2.遇到的问题


collectionView 在 reload 之后 self.visibleCells.count 是 0

经查找,需要先调用 [self layoutIfNeeded]; 才行

参考:

https://stackoverflow.com/questions/26055626/uicollectionview-visiblecells-returns-0-before-scrolling


3.通过实现代理方法来定制图片文字,以及代理返回的 emptyView 进行更多定制


>>>>> collectionView 的代理方法


@protocol JHNoDataUICollectionViewDelegate <NSObject>

@optional

/// offer a image to show some infomation for user.
- (UIImageView *)imageViewForCollectionViewWhenDataSourceIsEmpty;

/// offer a label to show some infomation for user.
- (UILabel *)labelForCollectionViewWhenDataSourceIsEmpty;

/// the empty view that add to tableView.
- (void)emptyViewForCollectionViewWhenDataSourceIsEmpty:(UIView *)emptyView;

@end


>>>>> tableView 的代理方法


@protocol JHNoDataUITableViewDelegate <NSObject>

@optional

/// offer a image to show some infomation for user.
- (UIImageView *)imageViewForTableViewWhenDataSourceIsEmpty;

/// offer a label to show some infomation for user.
- (UILabel *)labelForTableViewWhenDataSourceIsEmpty;

/// the empty view that add to tableView.
- (void)emptyViewForTableViewWhenDataSourceIsEmpty:(UIView *)emptyView;

@end


地址:

https://github.com/xjh093/JHNoDataEmptyViewForT-C


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪冷啊

你的鼓励是对我的认可!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值