這篇文章主要介紹了ios UITableView如何實(shí)現(xiàn)無(wú)數(shù)據(jù)加載占位圖片,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)專注于企業(yè)成都營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站重做改版、湖南網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5技術(shù)、商城建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)公司、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為湖南等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
具體如下:
國(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];
打完收工
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“ios UITableView如何實(shí)現(xiàn)無(wú)數(shù)據(jù)加載占位圖片”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
標(biāo)題名稱:iosUITableView如何實(shí)現(xiàn)無(wú)數(shù)據(jù)加載占位圖片
文章出自:http://www.chinadenli.net/article18/pescdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、定制網(wǎng)站、網(wǎng)站建設(shè)、域名注冊(cè)、面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)