给UITableView添加一个方法 我是写了一个类别
.h文件
#import <UIKit/UIKit.h>
@interface UITableView (NoData)
- (void)tableViewDisplayWitMsg:(NSString *)message ifNecessaryForRowCount:(NSUInteger)rowCount;
@end
.m文件
- (void)tableViewDisplayWitMsg:(NSString *)message ifNecessaryForRowCount:(NSUInteger)rowCount{
if (rowCount == 0) {
// Display a message when the table is empty
// 没有数据的时候,UILabel的显示样式
UILabel *messageLabel = [UILabel new];
messageLabel.text = message;
messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
messageLabel.textColor = [UIColor lightGrayColor];
messageLabel.textAlignment = NSTextAlignmentCenter;
[messageLabel sizeToFit];
self.backgroundView = messageLabel;
self.separatorStyle = UITableViewCellSeparatorStyleNone;
} else {
self.backgroundView = nil;
self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
}
}
使用:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
[tableView tableViewDisplayWitMsg:@"暂无数据" ifNecessaryForRowCount:self.dataSource.count];
return _dataSource.count;
}