新壁紙鎮(zhèn)樓
前言
其實沒什么可前言的拿愧、有圖誰嗶嗶是吧衅谷。
如果符合你的需求染服、再繼續(xù)往下看原理
正文
項目里之前的寫法是這樣的
- (void)checkData {
[self hideHUD];
[self.tableView reloadData];
[self.tableView.mj_header endRefreshing];
[self.tableView.mj_footer endRefreshing];
if (self.dataArray.count == 0) {
[self showNoteView];
}else {
[self removeNoteview];
}
}
然后具體是每個頁面創(chuàng)建一個NoteView。根據(jù)需要展示母债。
為什么這樣寫呢赋荆、我也不知道笋妥。反正一直按著公司舊代碼copy著來了。
這樣寫功能實現(xiàn)上并沒什么問題窄潭、實際使用也是春宣。過程簡單易懂。
不過嫉你、這樣寫月帝。肯定是有問題的:
1.確實是為了過程而過程了幽污。
2.每一個列表VC都要copy一份創(chuàng)建的代碼啊真的是一模一樣啊嚷辅。滿地都是啊。
3.也是最重要的距误。領(lǐng)導(dǎo)一直看著這個代碼塊很不順眼~~去年說過讓我解決一下但是我一直沒弄...不過簸搞、去年的任務(wù)總不能拖到春節(jié)以后對吧扁位?
哼(ˉ(∞)ˉ)唧 哼(ˉ(∞)ˉ)唧
解決實現(xiàn)
我們自然是希望以上需求與tableView綁定成為其固有功能。每次網(wǎng)絡(luò)請求結(jié)束之后趁俊、自動檢查數(shù)據(jù)源贤牛、而后處理。
網(wǎng)絡(luò)請求我們從tableView中無法直接跟蹤则酝、但只要把相應(yīng)操作注入到reloadData方法里就行了。
所以接下來闰集。swizzle(別忘了頭文件#import <objc/runtime.h>)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[[UITableView new] swizzleMethod:@selector(reloadData) withMethod:@selector(KTreloadData)];
});
}
- (void)swizzleMethod:(SEL)origSelector withMethod:(SEL)newSelector
{
Class class = [self class];
Method originalMethod = class_getInstanceMethod(class, origSelector);
Method swizzledMethod = class_getInstanceMethod(class, newSelector);
BOOL didAddMethod = class_addMethod(class,
origSelector,
method_getImplementation(swizzledMethod),
method_getTypeEncoding(swizzledMethod));
if (didAddMethod) {
class_replaceMethod(class,
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzledMethod);
}
}
現(xiàn)在我們成功的修改了reloadData方法的指針指向了KTreloadData
在KTreloadData方法中對操作進行注入就行了
- (void)KTreloadData {
[self KTreloadData];
if (self.noteView) {
[self checkDataSource];
}
}
關(guān)于這個if沽讹、由于Catergory會直接全局加載。要有個屬性判斷當前的talbeView是否需要繼續(xù)進行代碼注入才行武鲁。
//工作代碼~就這么幾行
- (void)checkDataSource {
//需求是沒有數(shù)據(jù)則不允許下拉刷新爽雄。如果不要阻隔下拉動作。則把self.noteView置于self上沐鼠、或者將self.noteView的層級調(diào)至self之下即可
id <UITableViewDataSource> dataSource = self.dataSource;
NSInteger numberOfSections = [dataSource numberOfSectionsInTableView:self];
for (int i = 0; i < numberOfSections; i++) {
// 任意一行有內(nèi)容挚瘟、則移除noteView
if ( [dataSource tableView:self numberOfRowsInSection:i] != 0) {
[self.noteView removeFromSuperview];
return;
}
}
// [self addSubview:self.noteView];
[self.superview addSubview:self.noteView];
然后、用起來是這樣的
[self.tableView addNoteViewWithpicName:@"bg_no_grab" noteText:@"我們的需求是btn刷新饲梭、硬要下拉刷新看類別里" refreshBtnImg:@"detail_btn_filladdress.png"];
就是最上面所展示的默認效果了(最后一個參數(shù)不填就不顯示btn了)乘盖。
當然、也可以自定義憔涉。
UILabel * customNoteView = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 100)];
customNoteView.numberOfLines = 0;
customNoteView.text = @"I am the Custom NoteView I am the Custom NoteView I am the Custom NoteView I am the Custom NoteView I am the Custom NoteView I am the Custom NoteView ";
self.tableView.noteView = customNoteView;
效果長這樣
最后
最近在學(xué)習前端所以不太有時間造輪子什么的订框、不過demo還是有的。
https://github.com/kiritoSong/KTTableViewEmptyDemo
如果大家需求差不多兜叨、拿回去自己改改比集成各種tableView效果的三方庫要舒服一些穿扳。反正不難、畢竟學(xué)到手了就是自己的嘛国旷。
PS:Catergory是個好東西矛物、可以幫我們提升很多權(quán)限解決很多問題。不過跪但、并不是所有時候都適用履羞、尤其是不要把建Catergory當成習慣~(嗯、不要問我怎么知道的)