一、前言
如果你還沒有看過下面的文章,可以花點時間看看
1.一行代碼完成“空TableView占位視圖”管理:
http://www.itdecent.cn/p/0a5f6b221ab6
原理:
1.獲取 Section 的數(shù)量
2.獲取每一個 Section 當中 Cell 的數(shù)量
2.UITableView沒數(shù)據(jù)時用戶提示如何做?
http://www.itdecent.cn/p/e39699b0d134
原理:
基于 [dataSource count];
一行代碼實現(xiàn)
3.iOS 0行代碼實現(xiàn) TableView 無數(shù)據(jù)時展示占位視圖
http://www.itdecent.cn/p/246b445ec4e3
原理:
1.獲取 Section 的數(shù)量
2.獲取每一個 Section 當中 Cell 的數(shù)量
二、正題:
1.原理
UITableView有一個屬性:
@property (nonatomic, readonly) NSArray<__kindof UITableViewCell *> *visibleCells;
UICollectionView有同樣的一個屬性:
@property (nonatomic, readonly) NSArray<__kindof UICollectionViewCell *> *visibleCells;
都是獲取可見的 Cell
可以根據(jù) reload 之后,可見 Cell 的數(shù)量來判斷,列表是否為空。
2.遇到的問題
collectionView 在 reload 之后 self.visibleCells.count 是 0
經(jīng)查找,需要先調用 [self layoutIfNeeded];才行
參考:
3.通過實現(xiàn)代理方法來定制圖片與文字,以及代理返回的 emptyView 進行更多定制
3.1collectionView 的代理方法
@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
3.2tableView 的代理方法
@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
地址: