因在項目中多次遇到了單選蝶防,多選的需求,所以在這里總結(jié)下明吩,以便下次可以有個參考或者直接可以用的间学。
swift版
在DEMO中主要實現(xiàn)了以下功能
- 分組,按鈕布局自適應(yīng)文字大小
- 單選多選印荔,每個組分別設(shè)置單選多選
- 設(shè)置默認選擇項
分組是根據(jù)每個組的 titleArr
來判斷的低葫,
傳入數(shù)據(jù)源
- (void)setContentView:(NSArray *)contenArr titleArr:(NSArray *)titleArr
進行分組
for (NSInteger i = 0 ; i < titleArr.count; ++i) {
//設(shè)置默認的值,使保存值的數(shù)組是按照group的順序來保存仍律,便于后面對相應(yīng)的group的值進行增改
[self.saveSelButValueArr addObject:@""];
[self.saveGroupIndexArr addObject:@""];
//設(shè)置每一組的值嘿悬,并返回最后一個frame
@autoreleasepool {
self.frameRect = [self setSignView:contenArr[i] andTitle:titleArr[i] andFrame:self.frameRect andGroupId:i];
}
}
設(shè)置滾動視圖的滾動范圍
self.scroller.contentSize = CGSizeMake(0, self.frameRect.size.height + self.frameRect.origin.y + 10);
文字自適應(yīng)
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;
設(shè)置默認選中
//設(shè)置默認選擇
if (_isDefaultSel) {
NSString *valueStr = [NSString stringWithFormat:@"%ld/%@",i,dataAr[i]];
//設(shè)置默認選擇以數(shù)組形式,則存在多選
if (_defaultSelectIndexArr.count > 0) {
//每個組單獨設(shè)置默認選中值
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{
//統(tǒng)一設(shè)置默認選擇值
if (i == _defaultSelectIndex) {
but.backgroundColor = _selColor;
but.selected = YES;
//保存默認選中按鈕的值
if (_singleFlagArr.count > 0) {
//為每個組設(shè)置單選還是多選
[self.saveSelButValueArr replaceObjectAtIndex:groupId withObject:[_singleFlagArr[groupId] isEqual:@0] ? @[valueStr] : valueStr];
}else{
[self.saveSelButValueArr replaceObjectAtIndex:groupId withObject:_isSingle ? valueStr : @[valueStr]];
}
}
}
單選水泉,多選
//多選善涨,主要是將選中的值保存到數(shù)組里
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];
}
//單選窒盐,用循環(huán)方式,確保每個單選組的都只有一個選中钢拧,不存在多個選中
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;
}
//取出當(dāng)前所在的組的一條數(shù)據(jù)蟹漓,因為單選,所以就只有一條數(shù)據(jù), 并拼接當(dāng)前選擇的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;
}
記錄到此結(jié)束源内,代碼簡單葡粒。
DEMO-GitHub-地址
DEMO-Bitbucket-地址