iOS多级选择框架封装与项目中的一些坑

###开篇 6月了啊,感觉堕落了一个月,哈哈。今天再更新一篇吧,主要内容,关于之前抽空整理的一个多级选择框架的封装和几个项目中遇到的奇怪的坑。 #####多级选择框架 实现的框架效果图如下:

多级筛选代码GSMultipleChoiceView地址

  • 主要功能: 实现左边存在多个选项,可以自适应滚动和设置字体的宽度和底部下划线的宽度,筛选的多级选择,可以记忆上次选中的效果,点击一级筛选,二级筛选关闭并刷新数据,可以根据点击的不同一级标题显示不同的二级标题的筛选名称。筛选左侧的一级标题可以大于4个,会自动滚动。
  • 调用方法 一级筛选的加载方法如下
 MultipleChoiceView =[[RedChipMultipleChoiceView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 45*ratio_width) titles:@[@"待展期", @"未到期", @"已结束",@"全部"] moreSelctBtnTitle:@"筛选" btn_w:SCREEN_WIDTH/5];
复制代码

遵循MultipleChoiceView 的代理,为了方便自定义,根据点击筛选之后创建二级标题的显示

#pragma mark - 头部控制的代理方法
//点击左边滚动的返回 点击的具体的点击按钮
-(void)redChipClicktWith:(NSNumber *)nowSelect{
    
    NSLog(@"-----%ld",[nowSelect integerValue]);
    //    [MultipleChoiceView.moreSelectBtn setTitleColor:[Toolkit getColor:hex_606060] forState:UIControlStateNormal];
    [MultipleChoiceView.moreSelectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    MultipleChoiceView.selcetBtnIsSelect = NO;
    MultipleChoiceView.rightView.image =[UIImage imageNamed:@"红筹优化新图标三角-下-@2x"];
    self.SlectbottomView.hidden =YES;
    
    currentSelctNum = nowSelect;
    
    //点击一级标题 下面的关闭  设置默认传参
    goodList_sort = @"1";
    goodsList_order = @"desc";
    
    //然后点击  请求网络 刷新tableview
    //此处根据点击后的返回数据 做网络的请求,界面的刷新等操作 
}
复制代码

点击筛选按钮创建二级标题处理相关逻辑

//点击筛选按钮
-(void)clickMoreSelcetBtn{
    if ([currentSelctNum integerValue] == 4) {
        arrName = @[@"参与时间",@"结束时间",@"奖励"];
    }else{
        arrName = @[@"参与时间",@"到期时间",@"奖励"];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self prepareMenuViewSubviews];
        
    });
    
    if (MultipleChoiceView.selcetBtnIsSelect) {
        //        [MultipleChoiceView.moreSelectBtn setTitleColor:[Toolkit getColor:hex_606060] forState:UIControlStateNormal];
        [MultipleChoiceView.moreSelectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        MultipleChoiceView.selcetBtnIsSelect = NO;
        MultipleChoiceView.rightView.image =[UIImage imageNamed:@"红筹优化新图标三角-下-@2x"];
        self.SlectbottomView.hidden =YES;
    }else{
        //        [MultipleChoiceView.moreSelectBtn setTitleColor:[Toolkit getColor:hex_ff5600] forState:UIControlStateNormal];
        [MultipleChoiceView.moreSelectBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        
        MultipleChoiceView.selcetBtnIsSelect = YES;
        MultipleChoiceView.rightView.image =[UIImage imageNamed:@"红筹优化新图标三角-上@2x"];
        self.SlectbottomView.hidden =NO;
    }
}
复制代码

实现效果如下: 演示gif02.gif ####项目中的坑 欢乐的时光总是短暂的接下来,说说最近项目中遇到的坑吧,最近负责模块较多,对老代码的更改可谓一步一坑啊,下面就从郁闷程度一点点说一下吧。

00

猝不及防的iOS10.3的坑

商城中对于商品的优惠价格处理的中划线失效。 原因:在10.3系统升级后(起码我是这个时候发现的),包含有中文价格或者中文符号的中划线富文本处理不起作用了。 之前的处理方式代码如下:

 NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr_origin = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"价格:¥%@",[goods_info objectForKey:@"market_price"]] attributes:attribtDic];
    self.current_price_lbl.attributedText=attribtStr_origin;
复制代码

新的解决方式如下:

    NSUInteger length = [[NSString stringWithFormat:@"价格:¥%@",[goods_info objectForKey:@"market_price"]] length];
    NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"价格:¥%@",[goods_info objectForKey:@"market_price"]]];
    [attri setAttributes:@{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle], NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle),NSForegroundColorAttributeName:[UIColor lightGrayColor]} range:NSMakeRange(3,length-3)];
    [self.current_price_lbl setAttributedText:attri];
		
复制代码

由代码可知,主要是增加一个富文本属性:

NSBaselineOffsetAttributeName : @(NSUnderlineStyleSingle)
复制代码

既然都到这里了,就索性再放两个平时常用的两种富文本处理方式 显示富文本段落:

 NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[shareTexe dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
 [self.midDesclabel setAttributedText:attrStr];
 [self.midDesclabel sizeToFit];
复制代码

对部分文字做富文本处理

cell.voucher_RedChipCountLabel.textColor = [Toolkit getColor:hex_606060];
NSMutableAttributedString* attribute=[[NSMutableAttributedString alloc] initWithString:indexRow_RedChipCountStr];
[attribute addAttribute:NSForegroundColorAttributeName value:[Toolkit getColor:hex_ff5600] range:NSMakeRange(5, indexRow_RedChipCountStr.length - 5)];
[attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, 5)];
[attribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(5, indexRow_RedChipCountStr.length - 5)];
cell.voucher_RedChipCountLabel.attributedText = attribute;

复制代码

######Zxing-Object二维码显示的坑 下面开始测试中没有遇到,但是用户反馈的坑

01

  • 发生场景,生成一张带二维码的图片,两种方式处理,一种是保存到本地识别,一种是直接分享到qq。
  • 问题:有部分用户二维码无法识别,生成的图片保存到本地直接识别可以识别,直接分享到qq,部分图片无法识别。 生成二维码方法如下:
    dispatch_async(dispatch_get_main_queue(), ^{
            
            if (str != nil) {
                ZXMultiFormatWriter *writer = [[ZXMultiFormatWriter alloc] init];
                ZXEncodeHints *hints = [ZXEncodeHints hints];
                hints.encoding = NSUTF8StringEncoding;
                hints.dataMatrixShape =ZXDataMatrixSymbolShapeHintForceSquare;
                hints.margin = [NSNumber numberWithInt:0.];
                
                hints.errorCorrectionLevel = [ZXQRCodeErrorCorrectionLevel errorCorrectionLevelH];
                NSString *qrString = str;
                ZXBitMatrix *result = [writer encode:qrString format:kBarcodeFormatQRCode width:500 height:500 hints:hints error:nil];
                
                if (result) {
                    CGImageRef image = [[ZXImage imageWithMatrix:result] cgimage];
                    UIImage *image1 =   [UIImage imageWithCGImage:image];//二维码原图
                    self.shareErWeiMaView.image =image1;
                    
                } else {
                    self.shareErWeiMaView.image = nil;
                }
            }
        });
复制代码

由于对生成二维码的容错等级和范围都做了设置,又并无logo遮挡,重点是保存到本地可以识别,所以比较奇怪,最终解决方式增大了范围。

  ZXBitMatrix *result = [writer encode:qrString format:kBarcodeFormatQRCode width:500 height:500 hints:hints error:nil];
复制代码

对上句代码中的范围做了增大,解决了部分二维码不能识别的问题。

#####未解之谜的神坑,心塞 一直未能复现,所以多说说。

  • bug来源:用户反馈
  • 原因:在多级筛选的时候,显示混乱,初次点击混乱,刷新一下正常,具体界面是,筛选的已结束界面的显示。

正常显示的已结束页面如下图:

正常已结束.png 正常的未结束包含倒计时的数据如下图:

正常倒计时.png 用户反馈bug,界面截图如下:

反馈图.png

由图可以看到,在已结束的时候显示了倒计时,对一个tableview来说,第一时间就是复用出了问题,或者控件的显示隐藏判断逻辑出了问题。 在老代码中分别用timeLabel 和endTimeLabel 来分别显示已结束的标题和倒计时的标题,通过后台的一个字段来控制标题的显示内容。 根据后台返回数据的不同的几种cell情况来决定timeLabel 和endTimeLabel的显示和隐藏。 这两个控件布局如下:

   [self.cell_iconImageView_1 makeConstraints:^(MASConstraintMaker* make){
        make.top.equalTo(self.contentView).offset(15*ratio_width);
        make.bottom.equalTo(self.contentView).offset(-15*ratio_width);
        make.left.equalTo(self.contentView).offset(10*ratio_width);
        make.width.equalTo(self.cell_ImageView_1.height);
    }];
    [self.timeLabel makeConstraints:^(MASConstraintMaker* make){
        make.top.equalTo(self.addRewardMoney.mas_bottom).offset(5*ratio_width);
        make.left.equalTo(self.cell_iconImageView_1.right).offset(10*ratio_width);
        make.right.equalTo(self.contentView);
        make.height.equalTo(19*ratio_width);
    }];
    [self.endTimeLabel makeConstraints:^(MASConstraintMaker* make){
        make.bottom.equalTo(self.contentView).offset(-10*ratio_width);
        make.left.equalTo(self.cell_iconImageView_1.right).offset(10*ratio_width);
        make.right.equalTo(self.contentView);
        make.height.equalTo(25*ratio_width);
    }];
复制代码

这两个控件都是根据图片,从左开始排列,并且存在一定交叉,我们故意让他们同时显示如下:

假设错误图.png 可以看到,就算是显示隐藏错误,出现时为止应该是上图所示,不应该存在如下图反馈图所显示的内容

反馈图标记 因此

  • 疑惑1:此倒计时为止压根没有控件存在,怎么会显示。
  • 疑惑2:控件的显示内容在每个cell时皆有判断不会出现上述情况。
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{}

              NSInteger status = [[NSString stringWithFormat:@"%@",[[_dataArray_ing objectAtIndex:indexPath.section] objectForKey:@"ostatus"]] integerValue];
                if (status == 2) {
                    
                    cell.timeLabel.text=@"已结束";
                    
                }else{
                 
                    if (seconds==0) {
                        if ([[[_dataArray_ing objectAtIndex:indexPath.section] objectForKey:@"activity"] intValue]==10) {
                            cell.timeLabel.text=@"已到期";
                        }
                        else
                        {
                            cell.timeLabel.text=@"待展期";
                        }
                    }else{
                        if ([_dataArray_ing count]>[totalLastTime count]&&[totalLastTime count]<(indexPath.section+1)) {
                            NSDictionary *dic = @{@"indexPath":[NSString stringWithFormat:@"%ld",indexPath.section],@"lastTime": [[_dataArray_ing objectAtIndex:indexPath.section] objectForKey:@"remain_time" ]};
                            [totalLastTime addObject:dic];
                            cell.timeLabel.text= [NSString stringWithFormat:@"剩余时间:%@",[Toolkit formatSecondToData:seconds]];
                        }
                    }

                }
复制代码
  • 疑惑3 开发过程中,多个测试账号,以及三天的测试周期中从未出现这种情况,再反馈后几个人多次测试仍未出现。
  • 疑惑4 据反馈说初次点击是这样上拉刷新恢复就更奇怪了,因为每次点击新的一级菜单就等于是一次新的刷新。

可惜的是并未反馈用户的账号信息,而且只有一个反馈,不然非拉着后台到底哪里的问题。

####后记 好久没更新,堕落的一个月,开始继续进步吧,哈哈,今天主要就说这些吧,希望封装对有需要的,以及遇到同样bug的有所帮助,话说今天的简书服务器是怎么了,显示打不开网页,写着写着又打不开了,忽然好怂,万一简书坏了,这些文章咋办。。。。等了半小时终于好了,终于好了。唉

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值