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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值