因在項(xiàng)目中多次遇到了單選,多選的需求肢执,所以在這里總結(jié)下枉阵,以便下次可以有個(gè)參考或者直接可以用的。
OC版
在DEMO中主要實(shí)現(xiàn)了以下功能
- 分組预茄,按鈕布局自適應(yīng)文字大小
- 單選多選兴溜,每個(gè)組分別設(shè)置單選多選
- 設(shè)置默認(rèn)選擇項(xiàng)
分組是根據(jù)每個(gè)組的 titleArr
來(lái)判斷的侦厚,
傳入數(shù)據(jù)源
func setDataSource(contetnArr : Array<Any>, titleArr : Array<String>)
進(jìn)行分組
//設(shè)置默認(rèn)的值,使保存值的數(shù)組是按照group的順序來(lái)保存拙徽,便于后面對(duì)相應(yīng)的group的值進(jìn)行增改
for (index,title) in titleArr.enumerated() {
saveSelButValueArr.append("")
saveSelGroupIndexeArr.append("")
frameRect = setupGroupAndStream(content: contetnArr[index] as! Array<Any>, titleStr: title, currFrame: frameRect, groupId: index)
}
設(shè)置滾動(dòng)視圖的滾動(dòng)范圍
//設(shè)置滾動(dòng)范圍
scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width, height: frameRect.size.height + frameRect.origin.y + 20)
文字自適應(yīng)
let but_width = calcuateLabSizeWidth(str: value as! String, font:content_titleFont, maxHeight: CGFloat(content_height)) + 20
//計(jì)算每個(gè)button的 X
margin_x = CGFloat(alineButWidth) + CGFloat(content_x)
//計(jì)算一行的寬度
alineButWidth = CGFloat(content_x) + but_width + CGFloat(alineButWidth)
//判斷是否需要換行
if alineButWidth >= self.frame.size.width{
margin_x = CGFloat(content_x)
alineButWidth = margin_x + but_width
content_totalHeight = current_rect.size.height + current_rect.origin.y + CGFloat(content_x)
}
// print("margin_x = \(margin_x)")
sender.frame = CGRect(x: margin_x, y: content_totalHeight, width: but_width, height: CGFloat(content_height))
//臨時(shí)保存frame刨沦,以進(jìn)行下一次坐標(biāo)計(jì)算
current_rect = sender.frame
設(shè)置默認(rèn)選中
//MARK:---設(shè)置默認(rèn)---單選
private func setDefaultSingleSelect(index : Int , groupId : Int ,value : String, sender : UIButton, content : Array<Any>){
//單選
let valueStr = "\(index)/\(value)"
if defaultSelSingleIndeArr.isEmpty{
assert( !(defaultSelIndex > content.count - 1), "在groupId = \(groupId) 設(shè)置默認(rèn)選中項(xiàng)不能超過(guò)\(content.count - 1)")
if index == defaultSelIndex{
sender.isSelected = true
sender.backgroundColor = content_backSelColor
saveSelButValueArr[groupId] = valueStr
}
}else{
assert(!((defaultSelSingleIndeArr[groupId] as? Int)! > content.count - 1), "在groupId = \(groupId) 設(shè)置默認(rèn)選中項(xiàng)不能超過(guò)\(content.count - 1)")
if index == defaultSelSingleIndeArr[groupId] as? Int{
sender.isSelected = true
sender.backgroundColor = content_backSelColor
saveSelButValueArr[groupId] = valueStr
}
}
saveSelGroupIndexeArr[groupId] = String(groupId)
}
//MARK:---設(shè)置默認(rèn)---多選
private func setDefaultMultipleSelect(index : Int , groupId : Int ,value : String, sender : UIButton, content : Array<Any>) -> Array<Any>{
let content = defaultSelIndexArr[groupId]
var tempSaveSelIndexArr = Array<Any>()
if content is Int{
if index == content as! Int{
sender.isSelected = true
sender.backgroundColor = content_backSelColor
tempSaveSelIndexArr.append("\(index)/\(value)")
}
}
if content is Array<Any>{
for contenIndex in content as! Array<Any>{
if index == contenIndex as! Int{
sender.isSelected = true
sender.backgroundColor = content_backSelColor
tempSaveSelIndexArr.append("\(index)/\(value)")
continue
}
}
}
saveSelGroupIndexeArr[groupId] = String(groupId)
return tempSaveSelIndexArr
}
單選,多選
//MARK:---單選
private func singalSelectEvent(sender : UIButton){
var valueStr : String = ""
let tempDetailArr = dataSourceArr[sender.tag / 100] as! Array<Any>
if sender.isSelected {
for (index, _) in tempDetailArr.enumerated(){
if index + 1 == sender.tag % 100{
sender.isSelected = true
sender.backgroundColor = content_backSelColor
continue
}
let norSender = scrollView.viewWithTag((sender.tag / 100) * 100 + index + 1) as! UIButton
norSender.isSelected = false
norSender.backgroundColor = content_backNorColor
}
valueStr = "\(sender.tag % 100 - 1)/\(tempDetailArr[sender.tag % 100 - 1])"
//閉包傳值
if currentSelValueClosure != nil {
currentSelValueClosure!(valueStr,sender.tag % 100 - 1,sender.tag / 100)
}
//代理傳值
delegate?.currentSelValueWithDelegate?(valueStr: valueStr, index: sender.tag % 100 - 1, groupId: sender.tag / 100)
}else{
sender.backgroundColor = content_backNorColor
}
//保存選中的值
saveSelButValueArr[sender.tag / 100] = valueStr
//保存groupId
saveSelButValueArr[sender.tag / 100] as! String == "" ? (saveSelGroupIndexeArr[sender.tag / 100] = "") : (saveSelGroupIndexeArr[sender.tag / 100] = String(sender.tag / 100))
}
//MARK:---多選
private func multipleSelectEvent(sender : UIButton){
var valueStr = ""
var tempSaveArr = Array<Any>()
if ((saveSelButValueArr[sender.tag / 100]) is Array<Any>){
tempSaveArr = saveSelButValueArr[sender.tag / 100] as! Array<Any>
}else{
tempSaveArr.append(saveSelButValueArr[sender.tag / 100])
}
let tempDetailArr = dataSourceArr[sender.tag / 100] as! Array<Any>
valueStr = "\(sender.tag % 100 - 1)/\(tempDetailArr[sender.tag % 100 - 1])"
if sender.isSelected {
sender.backgroundColor = content_backSelColor
//不存在相同的元素
tempSaveArr.append(valueStr)
//閉包傳值
if currentSelValueClosure != nil {
currentSelValueClosure!(valueStr,sender.tag % 100 - 1,sender.tag / 100)
}
//代理傳值
delegate?.currentSelValueWithDelegate?(valueStr: valueStr, index: sender.tag % 100 - 1, groupId: sender.tag / 100)
}else{
sender.backgroundColor = content_backNorColor
//獲取元素的下標(biāo)
let index : Int = tempSaveArr.index(where: {$0 as! String == valueStr})!
tempSaveArr.remove(at: index)
}
saveSelButValueArr[sender.tag / 100] = tempSaveArr
tempSaveArr.isEmpty ? (saveSelGroupIndexeArr[sender.tag / 100] = "") : (saveSelGroupIndexeArr[sender.tag / 100] = String(sender.tag / 100))
}
計(jì)算文字寬度
//MARK:---計(jì)算文字寬度
private func calcuateLabSizeWidth(str : String, font : UIFont, maxHeight : CGFloat) -> CGFloat{
let attributes = [kCTFontAttributeName: font]
let norStr = NSString(string: str)
let size = norStr.boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: maxHeight), options: .usesLineFragmentOrigin, attributes: attributes as [NSAttributedStringKey : Any], context: nil)
return size.width
}
記錄到此結(jié)束膘怕,代碼簡(jiǎn)單想诅。
DEMO-GitHub-地址
DEMO-Bitbucket-地址