NSString *advantageStr;//接收亮点标签

  NSArray *JobTagArry;//从后台返回的标签

NSMutableArray *selectArr;//接收点击的btn

 

进去没有标签 获取后台给的标签

CGFloat w = ([UIScreen mainScreen].bounds.size.width-20) / 3-10;

            CGFloat h = 30;

            CGFloat margin = 10;

            for (NSInteger i = 0; i < JobTagArry.count; i++) {

                

                UIButton *button = [[UIButton alloc] init];

                [cell3.linghtspotagScrollView addSubview:button];

                button.tag = i + 100;

                [button setTitle:((SJFourthVCModel *)(JobTagArry[i])).ra_name forState:UIControlStateNormal];

                [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];

                

                button.titleLabel.font = [UIFont systemFontOfSize:12];

                button.layer.cornerRadius = 15;

                button.layer.masksToBounds = YES;

                button.layer.borderColor = TABLEVIEWBCOLOR.CGColor;

                button.layer.borderWidth = 1;

                //[button setImage:[UIImage imageNamed:@"灰加"] forState:UIControlStateNormal];

                button.backgroundColor = TABLEVIEWBCOLOR;

                CGFloat x = ((button.tag - 100) % 3) * (w + margin) + margin;

                CGFloat y = ((button.tag - 100) / 3) * (h + margin) + margin;

                button.frame = CGRectMake(x, y, w, h);

                

                [button addTarget:self action:@selector(selectTitle:) forControlEvents:UIControlEventTouchUpInside];

                cell3.linghtspotagScrollView.contentSize = CGSizeMake(3 * w + 5 * margin + 0.5 * w, 0);

                button.selected = NO;

                if ([selectArr containsObject:[NSString stringWithFormat:@"%ld",i+100]]) {

                    [button setBackgroundColor:main_Color];

                    button.selected = YES;

                }

            }

 

#pragma mark -------标签点击

- (void)selectTitle:(UIButton *)button{

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:4];

    SJLightspotTagTableViewCell*cell = [_thirhTableView cellForRowAtIndexPath:indexPath];

    

    NSLog(@"%lu",(unsigned long)selectArr.count);

    if (!button.selected) {

 

        if (selectArr.count < 3) {

 

            [button setBackgroundColor:main_Color];

            [selectArr addObject:[NSString stringWithFormat:@"%ld",button.tag]];

        }else{

            NSString *indexStr = selectArr[0];

            NSInteger index = [indexStr integerValue];

            UIButton *btn = [cell.contentView viewWithTag:index];

            [btn setBackgroundColor:TABLEVIEWBCOLOR];

            btn.selected = NO;

            [selectArr removeObject:[NSString stringWithFormat:@"%ld",btn.tag]];

            [button setBackgroundColor:main_Color];

            [selectArr addObject:[NSString stringWithFormat:@"%ld",button.tag]];

        }

    }else{

 

        if ([selectArr containsObject:[NSString stringWithFormat:@"%ld",button.tag]]) {

             [button setBackgroundColor:TABLEVIEWBCOLOR];

         

            [selectArr removeObject:[NSString stringWithFormat:@"%ld",button.tag]];

        }

    }

 

    button.selected = !button.selected;

     NSMutableString *str = [NSMutableString string];

    for (id obj in cell.linghtspotagScrollView.subviews)  {

        if ([obj isKindOfClass:[UIButton class]]) {

            UIButton* theButton = (UIButton*)obj;

            

            if (theButton.selected == YES) {

                NSLog(@"%@",theButton.titleLabel.text);

 

                [str appendString:theButton.titleLabel.text];

                [str appendString:@","];

                NSLog(@"%@",str);

            }

          

        }

    }

    

    advantageStr =[NSString stringWithFormat:@"%@",str];

    NSLog(@"%@",advantageStr);

    

}

 

还有一种 是有标签的 

加上这一段

 

 进去 先获取到 点击的btn

 NSMutableArray*skillMutArryID;//选中的标签id

BOOL isbool = [skillMutArryID containsObject:((SJFourthVCModel *)(JobTagArry[i])).w_id];

                

                if (isbool == 1) {

                    button.backgroundColor = main_Color;

                    [button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];

                    [selectArr addObject:[NSString stringWithFormat:@"%ld",button.tag]];

                    NSLog(@"%@",selectArr);

                }