Closed e21169191 closed 5 months ago
你说的是支持多选选择器吧,多选一般用 UITableView 来实现好一点,UIPickerView 只适用于单选的交互
不是多选,也是单选。类似于里面已经有的“至今”,但是至今指的是[NSDate date]。如果里面多一个自定义的String,并且SelectValue支持该String就好了。目前是只支持具体的时间,选了后,再打开,不能自动选择到这个String的位置。
swift--自定义全年样式
func yearMonthInfo() -> [BRResultModel]? {
let yearSys = NSDate.init().br_year
if yearSys < 2023 {
toastMessage(msg: "系统时间错误")
return nil
}
var yearArr: [Int] = []
for year in 2023 ... yearSys {
yearArr.append(year)
}
// 默认月份信息
let monthArrDefault = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"]
// 当前月份
let monthMax = NSDate.init().br_month
var listArr: [BRResultModel] = []
var idx = 0
for year in yearArr {
let resModel = BRResultModel.init()
resModel.parentKey = "-1"
resModel.parentValue = ""
resModel.value = "\(year)年"
resModel.key = "\(year)"
listArr.append(resModel)
for month in monthArrDefault {
if year == yearSys {
if (Int(month) ?? 1) <= monthMax {
let model = BRResultModel.init()
model.parentKey = "\(year)"
model.parentValue = "\(year)年"
model.key = month
if month.elementsEqual("0") == true {
model.value = "全年"
} else {
model.value = "\(month)月"
}
listArr.append(model)
}
if selIndexArr.count == 0 {
selIndexArr.append(.init(integerLiteral: idx))
selIndexArr.append(.init(integerLiteral: monthMax))
}
} else {
let model = BRResultModel.init()
model.parentKey = "\(year)"
model.parentValue = "\(year)年"
model.key = month
if month.elementsEqual("0") == true {
model.value = "全年"
} else {
model.value = "\(month)月"
}
listArr.append(model)
}
}
idx += 1
}
return listArr
}
weak var weakSelf = self
let picker = BRStringPickerView.init(pickerMode: .componentLinkage)
picker.title = "选择月份"
picker.dataSourceArr = yearMonthInfo()
picker.selectIndexs = selIndexArr
BMLog.info(selIndexArr)
picker.numberOfComponents = 2
picker.resultModelArrayBlock = {
resultModelArr in
BMLog.info(resultModelArr)
let yearModel = resultModelArr?.first
let monthModel = resultModelArr?.last
weakSelf?.selIndexArr.removeAll()
weakSelf?.selIndexArr.append(.init(integerLiteral: yearModel?.index ?? 0))
weakSelf?.selIndexArr.append(.init(integerLiteral: monthModel?.index ?? 0))
var title = ""
if monthModel?.index == 0 {
// 全年
BMLog.info(yearModel?.value)
title = yearModel?.value ?? "2023年"
let date = NSDate.br_date(from: title, dateFormat: "yyyy年")
weakSelf?.selDateStr = NSDate.br_string(from: date!, dateFormat: "yyyy")
} else {
// 年-月
BMLog.info("\(yearModel?.value ?? "2023年")\(monthModel?.value ?? "1月")")
title = "\(yearModel?.value ?? "2023年")\(monthModel?.value ?? "1月")"
let date = NSDate.br_date(from: title, dateFormat: "yyyy年MM月")
weakSelf?.selDateStr = NSDate.br_string(from: date!, dateFormat: "yyyy-MM")
}
weakSelf?.monthBtn.setTitle(title, for: .normal)
weakSelf?.tableView.mj_header?.beginRefreshing()
}
// 设置自定义样式
let style = BRPickerStyle.init()
style.pickerColor = .bm_hex(0xF3F6FB)
style.pickerTextColor = colorBlack
style.separatorColor = .clear
style.pickerTextFont = UIFont.systemFont(ofSize: 16)
style.pickerHeight = 38
picker.pickerStyle = style
picker.show()
比如有些数据默认是加载全部年份或全部月份的,也可以选择具体的年月日等来筛选数据。地区也是经常会有这样的需求,可以选全部地区,也可以筛选单个地区的数据。希望博主能实现该功能。在目前的时间选择器里做修改,需要做的改动挺大的