美文网首页
iOS 为UITableView或UICollectionVie

iOS 为UITableView或UICollectionVie

作者: 豌豆大师 | 来源:发表于2019-11-14 19:59 被阅读0次

    背景:

    1.平常码业务时,会有在列表没值时展示占位图的需求,有时候需要展示图片及文字,有时候我们还需要为占位图添加点击事件等。
    2.在每次请求的时候手动调用代码去添加占位图太麻烦,也不现实,对代码质量也有影响。
    3.git上面也有相关的三方库,但第三方库一般会考虑各种使用场景,代码量大,会对包体积造成影响。
    4.此Demo已经满足正常需求,且使用非常方便,一句代码即可。

    使用示例

    只需要在初始化列表控件的时候,加入以下一句代码即可。

    // image有值,title为nil时,只展示图片
    // image为nil,title有值时,只展示文字
    // block为nil时,则代表不需要点击事件
    [_tableView setEmptyViewWithImage:[UIImage imageNamed:@"noresult"] title:@"某有数据啊,点击我再刷新一次看看?" eventBlock:^{
     NSLog(@"点击了,去刷新喽");
     }];
    

    实现技术:

    1.Runtime:类别关联属性,添加成员变量、方法交换
    2.枚举(空值界面类型)、映射(对代理回调的监听)、Block(对点击事件的回调)

    代码:

    UIScrollView+Empty.h
    //
    //  UIScrollView+Empty.h
    //
    //  Created by ylh on 2019/11/13.
    //  Copyright © 2019年 private. All rights reserved.
    //
    
    /*
     1、使用时只需调用此方法,即可在调用SEL: reloadData 时自动判断是否展示空值界面
     2、更多定制可根据此实现方案进行相对应拓展  一般会对emptyView 进行定制操作
     
     exp:
     
     [_tableView setEmptyViewWithImage:[UIImage imageNamed:@"noresult"] title:@"某有数据啊,点击我再刷新一次看看?" eventBlock:^{
     NSLog(@"点击了,去刷新喽");
     }];
     
     */
    
    #import <UIKit/UIKit.h>
    
    
    /**
     空值界面类型
    
     - EmptyImage: 只显示图片
     - EmptyTitle: 只显示文字
     - EmptyImageAndTitle: 显示图片和文字
     */
    typedef NS_ENUM(NSInteger,EmptyType) {
        EmptyImage,
        EmptyTitle,
        EmptyImageAndTitle
    };
    
    /**
     点击事件回调
     */
    typedef void(^EmptyEventBlock)(void);
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIScrollView (Empty)
    @property (nonatomic,strong)UIView *emptyView;
    @property (nonatomic,copy)EmptyEventBlock emptyEventBlock;
    /**
     设置空值界面
     根据传入内容决定展示风格:  图片、文字、文字和图片、是否添加点击事件
    
     @param image 展示图片 可为空
     @param title 展示文字 可为空
     @param block 点击事件block 可为空  为空则没有点击事件
     */
    - (void)setEmptyViewWithImage:(UIImage *_Nullable)image title:(NSString *_Nullable)title eventBlock:(EmptyEventBlock _Nullable)block;
    @end
    
    
    @interface UITableView (Empty)
    
    @end
    
    @interface UICollectionView (Empty)
    
    @end
    NS_ASSUME_NONNULL_END
    
    
    UIScrollView+Empty.m
    //
    //  UIScrollView+Empty.m
    //
    //  Created by ylh on 2019/11/13.
    //  Copyright © 2019年 private. All rights reserved.
    //
    
    #import "UIScrollView+Empty.h"
    #import <objc/runtime.h>
    
    static NSString *kEmptyViewKey = @"EmptyViewKey";
    static NSString *kEmptyEventBlockKey = @"EmptyEventBlockKey";
    
    @implementation UIScrollView (Empty)
    
    
    - (void)setEmptyView:(UIView *)emptyView {
        objc_setAssociatedObject(self, &kEmptyViewKey, emptyView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    - (UIView *)emptyView {
        return objc_getAssociatedObject(self, &kEmptyViewKey);
    }
    
    - (void)setEmptyEventBlock:(EmptyEventBlock)emptyEventBlock {
        objc_setAssociatedObject(self, &kEmptyEventBlockKey, emptyEventBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    - (EmptyEventBlock)emptyEventBlock {
        return objc_getAssociatedObject(self, &kEmptyEventBlockKey);
    }
    
    - (void)setEmptyViewWithImage:(UIImage *)image title:(NSString *)title eventBlock:(EmptyEventBlock)block {
        if (!self.emptyView) {
            
            EmptyType type;
            if (image && !title) {
                type = EmptyImage;
            }else if (!image && title) {
                type = EmptyTitle;
            }else if (image && title) {
                type = EmptyImageAndTitle;
            }else{
                NSLog(@"参数错误,不能使用此功能");
                return;
            }
            
            UIButton *emptyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            block ? (emptyBtn.enabled = YES) : (emptyBtn.enabled = NO);
            (emptyBtn.enabled = YES) ? [emptyBtn addTarget:self action:@selector(emptyClick:) forControlEvents:UIControlEventTouchUpInside]:nil;
            (emptyBtn.enabled = YES) ? (self.emptyEventBlock = block) : nil;
            
            switch (type) {
                case EmptyImage:
                {
                    emptyBtn.frame = CGRectMake((self.frame.size.width-image.size.width)/2, (self.frame.size.height-image.size.height)/2, image.size.width, image.size.height);
                    [emptyBtn setImage:image forState:UIControlStateNormal];
                }
                    break;
                case EmptyTitle:
                {
                    CGRect titleRect = [title boundingRectWithSize:CGSizeMake(self.frame.size.width, 20) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16.0f],NSFontAttributeName, nil] context:nil];
                    emptyBtn.frame = CGRectMake((self.frame.size.width-titleRect.size.width)/2, (self.frame.size.height-titleRect.size.height)/2, titleRect.size.width, titleRect.size.height);
                    [emptyBtn setTitle:title forState:UIControlStateNormal];
                    [emptyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                }
                    break;
                case EmptyImageAndTitle:
                {
                    CGFloat btnWidth = 0;
                    CGFloat btnHeight = 0;
                    UIFont *titleFont = [UIFont systemFontOfSize:16.0f];
                    CGRect titleRect = [title boundingRectWithSize:CGSizeMake(self.frame.size.width, 20) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:titleFont,NSFontAttributeName, nil] context:nil];
                    titleRect.size.width <= image.size.width ? (btnWidth = image.size.width) : (btnWidth = titleRect.size.width);
                    btnHeight = image.size.height + titleRect.size.height + 10;
                    emptyBtn.frame = CGRectMake((self.frame.size.width-btnWidth)/2, (self.frame.size.height-btnHeight)/2, btnWidth, btnHeight);
                    [emptyBtn setImage:image forState:UIControlStateNormal];
                    [emptyBtn setTitle:title forState:UIControlStateNormal];
                    [emptyBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                    [emptyBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 30, -titleRect.size.width)];
                    [emptyBtn setTitleEdgeInsets:UIEdgeInsetsMake(btnHeight-20, -image.size.width, 0, 0)];
                    emptyBtn.titleLabel.font = titleFont;
                }
                    break;
                default:
                    break;
            }
            self.emptyView = emptyBtn;
            [self addSubview:self.emptyView];
        }
        [self bringSubviewToFront:self.emptyView];
    }
    
    /// 更新界面,是否展示空值视图
    - (void)refreshEmptyView {
        NSInteger section = 1;
        NSInteger rows = 0;
        if ([self isKindOfClass:[UITableView class]]) {
            
            UITableView *tableView = (UITableView *)self;
            if (tableView.dataSource != nil && [tableView.dataSource respondsToSelector:@selector(tableView:numberOfRowsInSection:)]) {
                
                if ([tableView.dataSource respondsToSelector:@selector(numberOfSectionsInTableView:)]) {
                    section = [tableView.dataSource numberOfSectionsInTableView:tableView];
                }
                for (int i = 0; i < section; i++) {
                    rows += [tableView.dataSource tableView:tableView numberOfRowsInSection:i];
                }
            }
        }else if ([self isKindOfClass:[UICollectionView class]]) {
            
            UICollectionView *collectionView = (UICollectionView *)self;
            if (collectionView.dataSource != nil && [collectionView.dataSource respondsToSelector:@selector(collectionView:numberOfItemsInSection:)]) {
                
                if ([collectionView.dataSource respondsToSelector:@selector(numberOfSectionsInCollectionView:)]) {
                    section = [collectionView.dataSource numberOfSectionsInCollectionView:collectionView];
                }
                for (int i = 0; i < section; i++) {
                    rows += [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:i];
                }
            }
        }
        
        if (rows == 0) {
            self.emptyView.hidden = NO;
            [self bringSubviewToFront:self.emptyView];
        }else{
            self.emptyView.hidden = YES;
            return;
        }
    }
    
    #pragma mark -- action
    
    - (void)emptyClick:(UIButton *)sender {
        if (self.emptyEventBlock) {
            self.emptyEventBlock();
        }
    }
    
    #pragma mark -- swizzing
    + (void)hookClass:(Class)classObject originalSelector:(SEL)originalSelector swizzledSelector:(SEL)swizzledSelector {
        Class class = classObject;
        Method originalMethod = class_getInstanceMethod(class, originalSelector);
        Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
        
        BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        if (didAddMethod) {
            originalMethod = class_getInstanceMethod(class, originalSelector);
        }
        method_exchangeImplementations(swizzledMethod, originalMethod);
    }
    
    @end
    
    
    @implementation UITableView (Empty)
    
    + (void)load {
        [super load];
        [self hookClass:[self class] originalSelector:@selector(reloadData) swizzledSelector:@selector(customReloadData)];
    }
    - (void)customReloadData {
        [self customReloadData];
        [self refreshEmptyView];
    }
    
    @end
    
    
    @implementation UICollectionView (Empty)
    
    + (void)load {
        [super load];
        [self hookClass:[self class] originalSelector:@selector(reloadData) swizzledSelector:@selector(customReloadData)];
    }
    
    - (void)customReloadData {
        [self customReloadData];
        [self refreshEmptyView];
    }
    
    @end
    
    

    如果对你帮助或者思路上的启发,帮忙点个赞呗!

    相关文章

      网友评论

          本文标题:iOS 为UITableView或UICollectionVie

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