美文网首页
iOS 分类的应用(UITableView请求返回数据为空时展

iOS 分类的应用(UITableView请求返回数据为空时展

作者: xu1Peng | 来源:发表于2021-09-17 18:11 被阅读0次

分类模块代码:

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN
@interface UITableView (category)
- (void)tableviewShowWithMessage:(NSString *)messsage forDataCount:(NSInteger)dataCount;
@end


#import "UITableView+category.h"

@implementation UITableView (category)

- (void)tableviewShowWithMessage:(NSString *)messsage forDataCount:(NSInteger)dataCount{
    if (dataCount == 0) {
        
        UIView *view = [UIView new];
            
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Bitmap"]];
        imgView.center = self.center;
        [view addSubview:imgView];
        
        UILabel *showLab = [UILabel new];
        showLab.text = messsage;
        showLab.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
        showLab.textColor = [UIColor blackColor];
        showLab.textAlignment = NSTextAlignmentCenter;
        [showLab sizeToFit];
        // 这块由于不想增加更多的代码(第三方、分类),showLab的Y值是根据图片的高度手动算的,需注意。
        [showLab setFrame:CGRectMake(20, self.center.y+60, [UIScreen mainScreen].bounds.size.width-40, 20)];
        [view addSubview:showLab];
        
        self.backgroundView = view;
    }else{
        self.backgroundView = nil;
    }
}

请求返回为空时调用:

 [self.tableview tableviewShowWithMessage:@"暂无数据" forDataCount:self.dataArray.count];
 [self.tableview reloadData];

相关文章

网友评论

      本文标题:iOS 分类的应用(UITableView请求返回数据为空时展

      本文链接:https://www.haomeiwen.com/subject/pulsgltx.html