iOS 商品筛选模块

分享一下商品通过综合,销量,价格等筛选模块,仅供参考,效果图如下:
在这里插入图片描述
上代码.h文件:

@class HBSalesPriceTypeView;

typedef NS_ENUM(NSInteger,HBButtonClickType){
    HBButtonClickTypeNormal = 0,
    HBButtonClickTypeUp = 1,
    HBButtonClickTypeDown = 2,
};
@protocol HBSalesPriceTypeViewDelegate <NSObject>
@optional
//选中最上方的按钮的点击事件
- (void)selectTopButton:(HBSalesPriceTypeView *)selectView withIndex:(NSInteger)index withButtonType:(HBButtonClickType )type;
@end
@interface HBSalesPriceTypeView : UIView
@property (nonatomic, weak) id<HBSalesPriceTypeViewDelegate>delegate;
//默认选中,默认是第一个
@property (nonatomic, assign) int defaultSelectIndex;
@end

.m文件

#import <objc/runtime.h>
#import <Masonry.h>
#import "UIButton+ImageTitleStyle.h"
static char *const btnKey = "btnKey";

// 获取RGBA颜色
#define RGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
#define SCREEN_WIDTH            ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT           ([[UIScreen mainScreen] bounds].size.height)

@interface HBSalesPriceTypeView ()
@property (nonatomic ,strong)NSArray *titleArr;//标题
@end
@implementation HBSalesPriceTypeView
-(NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = @[@"综合",@"销量",@"价格",@"高佣"];
    }
    return _titleArr;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        //添加子视图
        [self addSubViewUI];
    }
    return self;
}
#pragma mark ==========添加子视图==========
-(void)addSubViewUI{
    self.backgroundColor = [UIColor whiteColor];
    UIView *mainView = [UIView new];
    mainView.backgroundColor = RGBA(255, 255, 255, 1);
    [self addSubview:mainView];
    [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.right.top.equalTo(self);
    }];
    NSInteger count = self.titleArr.count;
    for (int i = 0; i < count; i++) {
        UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:self.titleArr[i] forState:UIControlStateNormal ];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [mainView addSubview:button];
        button.tag = 100+i;
        //布局
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(mainView).offset(SCREEN_WIDTH/count*i);
            make.top.bottom.equalTo(mainView);
            make.width.mas_equalTo(SCREEN_WIDTH/count);
        }];
        //设置默认选择
        if (i == _defaultSelectIndex) {
            button.selected = YES;
        }
        if (i==0) {
            //综合
        }else{
           [button setImage:[UIImage imageNamed:@"type_sale_shangxia"] forState:UIControlStateNormal];
            [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
            objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
        }
    }
}

#pragma mark ==========点击按钮==========
- (void)selectClick:(UIButton *)btn{
    
    for (int i = 0; i<4 ;i++) {
        UIButton *button = [self viewWithTag:i+100];
        button.selected = NO;
        if (i+100==btn.tag) {
            //当前点击按钮
            NSLog(@"点击当前按钮状态");
        }else{
            if (i==0) {
                //综合
            }else{
               [button setImage:[UIImage imageNamed:@"type_sale_shangxia"] forState:UIControlStateNormal];
                [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
                objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
            }
        }
    }
    btn.selected = YES;

    HBButtonClickType type = HBButtonClickTypeNormal;
    if (btn.tag == 100) {
        //综合
    }else{
       NSString *flag = objc_getAssociatedObject(btn, btnKey);
       if ([flag isEqualToString:@"1"]) {
           [btn setImage:[UIImage imageNamed:@"ec_type_sale_shang"] forState:UIControlStateNormal];
           objc_setAssociatedObject(btn, btnKey, @"2", OBJC_ASSOCIATION_ASSIGN);
           type = HBButtonClickTypeUp;
       }else if ([flag isEqualToString:@"2"]){
           [btn setImage:[UIImage imageNamed:@"ec_type_sale_xie"] forState:UIControlStateNormal];
           objc_setAssociatedObject(btn, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
           type = HBButtonClickTypeDown;
       }
    }
    
    if ([self.delegate respondsToSelector:@selector(selectTopButton:withIndex:withButtonType:)]) {
        [self.delegate selectTopButton:self withIndex:btn.tag withButtonType:type];
    }
    
}
@end

使用方法:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.selectTypeView];
    [self.selectTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(64);
        make.height.mas_equalTo(40);
        make.left.right.equalTo(self.view);
    }];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.selectTypeView.mas_bottom);
        make.bottom.equalTo(self.view);
        make.left.right.equalTo(self.view);
    }];
}

#pragma mark ==========getter==========
-(HBSalesPriceTypeView *)selectTypeView{
    if (!_selectTypeView) {
        _selectTypeView = [[HBSalesPriceTypeView alloc]initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 40)];
        _selectTypeView.delegate = self;
    }
    return _selectTypeView;
}
#pragma mark ==========代理==========
-(void)selectTopButton:(HBSalesPriceTypeView *)selectView withIndex:(NSInteger)index withButtonType:(HBButtonClickType)type{
    if (index==100) {
        //综合
 
    }else if (index==101){
        //销量
        
    }else if (index==102){
        //价格
        
    }else if (index==103){
        //佣金
    }
    
    //请求数据
}

附上demo

END.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明似水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值