UITableView沒資料時提示沒有更多資料
做專案的時候,用mj_footer提示沒有更多資料,但是專案有篩選功能,進行篩選後,沒有資料的情況下,是不會呼叫[_searchTableView.mj_footer endRefreshingWithNoMoreData];方法的;
解決辦法:
由於專案多處用到了該功能,所以進行了封裝(類擴充套件)。程式碼如下:
//.h檔案
#import <UIKit/UIKit.h>
@interface UITableView (EmptyData)
/**tableView資料為空的時候進行提示*/
- (void)tableViewDisplayWithText:(NSString *)text ifNecessoryForRowCount:(NSUInteger)rowCount;
@end
//.m檔案
#import "UITableView+EmptyData.h"
@implementation UITableView (EmptyData)
- (void)tableViewDisplayWithText:(NSString *)text ifNecessoryForRowCount:(NSUInteger)rowCount {
if (rowCount == 0) {
//沒有資料的時候,UILabel的顯示樣式
UILabel *textLabel = [UILabel new];
textLabel.text = text;
textLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
textLabel.textColor = [UIColor lightGrayColor];
textLabel.textAlignment = NSTextAlignmentCenter;
[textLabel sizeToFit];
self.backgroundView = textLabel;
self.separatorStyle = UITableViewCellSeparatorStyleNone;
} else {
self.backgroundView = nil;
self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
@end
然後倒入標頭檔案,再呼叫就可以了,(由於自己專案中cell就一個section,所以沒在section的方法裡面寫,如果是多個section,可以直接寫在section代理方法中)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"資料數量--%ld",(long)self.dataArr.count);
[tableView tableViewDisplayWithText:@"沒有查詢到相對應的商品" ifNecessoryForRowCount:_dataArr.count];
return self.dataArr.count;
}