IOS实现分组单选,多选,标签流

图片

因在项目中多次遇到了单选,多选的需求,所以在这里总结下,以便下次可以有个参考或者直接可以用的。

swift版

在DEMO中主要实现了以下功能
  • 分组,按钮布局自适应文字大小
  • 单选多选,每个组分别设置单选多选
  • 设置默认选择项

分组是根据每个组的 titleArr 来判断的,
传入数据源
- (void)setContentView:(NSArray *)contenArr titleArr:(NSArray *)titleArr

进行分组
for (NSInteger i = 0 ; i < titleArr.count; ++i) {
        //设置默认的值,使保存值的数组是按照group的顺序来保存,便于后面对相应的group的值进行增改
        [self.saveSelButValueArr addObject:@""];
        [self.saveGroupIndexArr addObject:@""];
        //设置每一组的值,并返回最后一个frame
         @autoreleasepool {
             self.frameRect = [self setSignView:contenArr[i] andTitle:titleArr[i] andFrame:self.frameRect andGroupId:i];
         }
    }
设置滚动视图的滚动范围
    self.scroller.contentSize = CGSizeMake(0, self.frameRect.size.height + self.frameRect.origin.y + 10);
文字自适应
 CGFloat butWidth = contentSize.width + 20;
 butorignX = alineButWidth + _maragin_x;
 alineButWidth = _maragin_x + butWidth + alineButWidth;
if (alineButWidth >= self.width) {
      butorignX = _maragin_x;
      alineButWidth = butorignX + butWidth;
      but_totalHeight = current_rect.size.height + current_rect.origin.y + _maragin_y;
   }
  but.frame = CGRectMake(butorignX, but_totalHeight, butWidth, butHeight);
  current_rect = but.frame;
设置默认选中
//设置默认选择
            if (_isDefaultSel) {
                 NSString *valueStr = [NSString stringWithFormat:@"%ld/%@",i,dataAr[i]];
                //设置默认选择以数组形式,则存在多选
                if (_defaultSelectIndexArr.count > 0) {
                    //每个组单独设置默认选中值
                    NSArray * selIndexArr = nil;
                    NSNumber * indexNumber = nil;
                    [_defaultSelectIndexArr[groupId] isKindOfClass:[NSArray class]] ? (selIndexArr = _defaultSelectIndexArr[groupId]) : (indexNumber = _defaultSelectIndexArr[groupId]);
                    if (selIndexArr.count > 0) {
                        for (NSNumber * selIndex in selIndexArr) {
                            if (i == [selIndex integerValue]) {
                                but.selected = YES;
                                but.backgroundColor = _selColor;
                                [tempSelArr addObject:valueStr];
                                break;
                            }
                        }
                    }else{
                        if (i == [indexNumber integerValue]) {
                            but.selected = YES;
                            [tempSelArr addObject:valueStr];
                            but.backgroundColor = _selColor;
                        }
                    }

                }else{
                    //统一设置默认选择值
                    if (i == _defaultSelectIndex) {
                        but.backgroundColor = _selColor;
                        but.selected = YES;
                        //保存默认选中按钮的值
                        if (_singleFlagArr.count > 0) {
                            //为每个组设置单选还是多选
                            [self.saveSelButValueArr replaceObjectAtIndex:groupId withObject:[_singleFlagArr[groupId] isEqual:@0] ? @[valueStr] : valueStr];
                        }else{
                            [self.saveSelButValueArr replaceObjectAtIndex:groupId withObject:_isSingle ? valueStr : @[valueStr]];
                        }
                    }
                }
单选,多选
//多选,主要是将选中的值保存到数组里
valueStr = [NSString stringWithFormat:@"%ld/%@",sender.tag % 100 - 1,self.dataSourceArr[sender.tag / 100][sender.tag % 100 - 1]];
        sender.backgroundColor = _selColor;
        if (![tempSaveArr containsObject:valueStr]) {
            [tempSaveArr addObject:valueStr];
        }else{
            [tempSaveArr replaceObjectAtIndex:sender.tag % 100 - 1 withObject:valueStr];
        }
//单选,用循环方式,确保每个单选组的都只有一个选中,不存在多个选中
 for (NSInteger i = 0; i < [self.dataSourceArr[sender.tag / 100] count]; ++i) {
                if (sender.tag % 100 ==  i + 1) {
                    sender.selected = YES;
                    sender.backgroundColor = _selColor;
                    continue;
                }
                UIButton *but = (UIButton *)[self.scroller viewWithTag:(sender.tag / 100) * 100 + i + 1];
                but.selected = NO;
                but.backgroundColor = _norColor;
            }
        //取出当前所在的组的一条数据,因为单选,所以就只有一条数据, 并拼接当前选择的Index
       valueStr = [NSString stringWithFormat:@"%ld/%@",sender.tag % 100 - 1,self.dataSourceArr[sender.tag / 100][sender.tag % 100 - 1]];
计算文字宽度
- (CGSize)sizeWithWidth:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)height{
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    attrDict[NSFontAttributeName] = font;
    CGSize size = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil].size;
    return size;
}

记录到此结束,代码简单。
DEMO-GitHub-地址
DEMO-Bitbucket-地址

swift版

你可能感兴趣的:(IOS实现分组单选,多选,标签流)