UITableView没数据时提示没有更多数据

来源:互联网 发布:上海 游戏程序员招聘 编辑:程序博客网 时间:2024/05/23 12:54

做项目的时候,用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;
}



0 0
原创粉丝点击