ios UITableView實(shí)現(xiàn)無數(shù)據(jù)加載占位圖片
本文介紹了ios UITableView實(shí)現(xiàn)無數(shù)據(jù)占位圖片,分享給大家,具體如下:
國(guó)際慣例,上效果圖

該效果的實(shí)現(xiàn)主要是使用runtime的交叉方法實(shí)現(xiàn),將tableView的reloadData與自定義的kk_reloadData交換。新建tableView的Category。
交換方法主要代碼
+ (void)swizzleInstanceSelector:(SEL)originalSel
WithSwizzledSelector:(SEL)swizzledSel {
Method originMethod = class_getInstanceMethod(self, originalSel);
Method swizzedMehtod = class_getInstanceMethod(self, swizzledSel);
BOOL methodAdded = class_addMethod(self, originalSel, method_getImplementation(swizzedMehtod), method_getTypeEncoding(swizzedMehtod));
if (methodAdded) {
class_replaceMethod(self, swizzledSel, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
}else{
method_exchangeImplementations(originMethod, swizzedMehtod);
}
}
交換reloadData
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self swizzleInstanceSelector:@selector(reloadData) WithSwizzledSelector:@selector(kk_reloadData)];
});
}
kk_reloadData方法,先檢查是否有數(shù)據(jù),再次kk_reloadData方法此時(shí)已使用runtime的交換方法則則實(shí)際上調(diào)用的是系統(tǒng)的reloadData方法。
- (void)kk_reloadData {
[self kk_checkEmpty];
[self kk_reloadData];
}
kk_checkEmpty方法
- (void)kk_checkEmpty {
BOOL isEmpty = YES;
id<UITableViewDataSource> src = self.dataSource;
NSInteger sections = 1;
if ([src respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
sections = [src numberOfSectionsInTableView:self];
}
for (int i = 0; i < sections; i++) {
NSInteger rows = [src tableView:self numberOfRowsInSection:i];
if (rows) {
isEmpty = NO;
}
}
if (isEmpty) {//數(shù)據(jù)為空,在這里添加視圖
}else{//數(shù)據(jù)不為空,在這里一處視圖
}
}
為了降低代碼的侵入,可以給tableView動(dòng)態(tài)添加一個(gè)View屬性即是占位圖視圖。
@property (nonatomic, strong) UIView *placeHolderView;
- (void)setPlaceHolderView:(UIView *)placeHolderView {
objc_setAssociatedObject(self, @selector(placeHolderView), placeHolderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)placeHolderView {
return objc_getAssociatedObject(self, @selector(placeHolderView));
}
kk_checkEmpty的
if (isEmpty) {//數(shù)據(jù)為空,在這里添加視圖
}else{//數(shù)據(jù)不為空,在這里一處視圖
}
修改為
if (isEmpty) {
[self.placeHolderView removeFromSuperview];
[self addSubview:self.placeHolderView];
}else{
[self.placeHolderView removeFromSuperview];
}
以后使用的時(shí)候只需設(shè)置tableView的placeHolderView屬性即可
_tableView.placeHolderView = [[UIView alloc] init];
打完收工
github地址: https://github.com/wuzaozhou/UITableView-placeholder
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺析iOS應(yīng)用開發(fā)中線程間的通信與線程安全問題
這篇文章主要介紹了淺析iOS應(yīng)用開發(fā)中線程間的通信與線程安全問題,談到了包括互斥鎖的使用等設(shè)計(jì)要點(diǎn),需要的朋友可以參考下2015-11-11
iOS開發(fā)之如何通過PUT請(qǐng)求上傳數(shù)據(jù)
眾所周知一般的服務(wù)器上傳數(shù)據(jù)都是用POST請(qǐng)求,這樣通過AFNetworking的POST請(qǐng)求穩(wěn)穩(wěn)的,但是有一天遇到一個(gè)問題,服務(wù)器上傳數(shù)據(jù)用的是PUT請(qǐng)求,發(fā)現(xiàn)用AFNetworking并不是那么好用,下面這篇文章就來講一下如何通過PUT請(qǐng)求上傳數(shù)據(jù)。有需要的朋友們可以參考借鑒。2016-11-11
iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問題解決的方法
這篇文章主要給大家介紹了關(guān)于iOS中3DTouch預(yù)覽導(dǎo)致TableView滑動(dòng)卡頓問題解決的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)同樣遇到的朋友們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起看看吧。2018-03-03
iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)簡(jiǎn)易的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01

