我想自动选择中间单元格。如果我选择开始时间和结束时间,就像截图一样,这里我选择01:00作为< strong>开始时间 和 06:00 作为结束时间。我想选择自动选择时间02:00、03:00、04:00、05:00,例如...
也许我的客户想相应地选择01:00 作为开始时间 和21:00 作为结束时间,自动 应该始终被选中。
我有三个时间范围,我需要为工作和每个范围时间显示不同的时间 strong> 我有三种价格。例如:
第一次和价格 早上。
第二次和价格 天。
第三次和价格 晚上。
我可以做什么以及如何做?也许有框架可以帮助我快速做到这一点?
我的代码在这里:
class BookingViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
var bookingHall: Halls?
var timeIntervalArray: [String] = []
var timeIntervalArrayTwo: [String] = []
var timeIntervalArrayThree: [String] = []
var selectedTimeIntervalArray: [String] = []
var bookingAmountOfHallArray: [Int] = []
var sumOfBookingAmount: Int = 0
var allArrayTimesAndPrices: [(time: String, price: Int)] = [] // ARRAY FOR COLLECT ALL TIMES AND ALL PRICES
lazy var timeFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
configureSelectedDateAndTime()
collectionView.allowsMultipleSelection = true
// MORNING TIME RANGE
if let timeRange = generateTimeRange() {
self.timeIntervalArray = timeRange
self.collectionView.reloadData()
} else {
// Handle error
}
// DAY TIME RANGE
if let timeRangeTwo = generateTimeRangeTwo() {
self.timeIntervalArrayTwo = timeRangeTwo
self.collectionView.reloadData()
} else {
// Handle error
}
// EVENING TIME RANGE
if let timeRangeThree = generateTimeRangeThree() {
self.timeIntervalArrayThree = timeRangeThree
self.collectionView.reloadData()
} else {
// Handle error
}
if let hall = bookingHall {
allArrayTimesAndPrices =
timeIntervalArray.map({ (time: $0, price: hall.price) }) +
timeIntervalArrayTwo.map({ (time: $0, price: hall.priceSecond) }) +
timeIntervalArrayThree.map({ (time: $0, price: hall.priceThird) })
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return allArrayTimesAndPrices.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath) as! BookingTimeCell
cell.timeLabel.text = allArrayTimesAndPrices[indexPath.item].time
cell.priceLabel.text = "\(allArrayTimesAndPrices[indexPath.item].price) руб."
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let selectedCell = collectionView.cellForItem(at: indexPath)
selectedIndexesAndBackgroundColor(cell: selectedCell!, indexPath: inde xPath)
}
func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
let deselectedCell = collectionView.cellForItem(at: indexPath)
deselectedIndexesAndBackgroundColor(cell: deselectedCell!, indexPath: indexPath)
}
// the selected time
func selectedIndexesAndBackgroundColor(cell: UICollectionViewCell, indexPath: IndexPath) {
let timeToAdd = allArrayTimesAndPrices[indexPath.item].time
selectedTimeIntervalArray.append(timeToAdd)
}
// cancelled time
func deselectedIndexesAndBackgroundColor(cell: UICollectionViewCell, indexPath: IndexPath) {
let timeToRemove = allArrayTimesAndPrices[indexPath.item].time
let indexTime = selectedTimeIntervalArray.index(of: timeToRemove)
selectedTimeIntervalArray.remove(at: indexTime!)
}
// MORNING
func generateTimeRange() -> [String]? {
var result = [String]()
if let hall = bookingHall {
guard var startTime = timeFormatter.date(from: hall.firstStartTimeToIncreasePrice) else { return nil }
guard let endTime = timeFormatter.date(from: hall.firstEndTimeToIncreasePrice) else { return nil }
while startTime <= endTime {
result.append(timeFormatter.string(from: startTime))
startTime = Calendar.current.date(byAdding: .hour, value: 1, to: startTime)!
}
}
return result
}
// DAY
func generateTimeRangeTwo() -> [String]? {
var result = [String]()
if let hall = bookingHall {
guard var startTime = timeFormatter.date(from: hall.secondStartTimeToIncreasePrice) else { return nil }
guard let endTime = timeFormatter.date(from: hall.secondEndTimeToIncreasePrice) else { return nil }
while startTime <= endTime {
result.append(timeFormatter.string(from: startTime))
startTime = Calendar.current.date(byAdding: .hour, value: 1, to: startTime)!
}
}
return result
}
// EVENING
func generateTimeRangeThree() -> [String]? {
var result = [String]()
if let hall = bookingHall {
guard var startTime = timeFormatter.date(from: hall.thirdStartTimeToIncreasePrice) else { return nil }
guard let endTime = timeFormatter.date(from: hall.thirdEndTimeToIncreasePrice) else { return nil }
while startTime <= endTime {
result.append(timeFormatter.string(from: startTime))
startTime = Calendar.current.date(byAdding: .hour, value: 1, to: startTime)!
}
}
return result
}
}
最佳答案
创建以下全局属性:
var selectedIndexPath:IndexPath! // store selected indexpath
var arrSelectedIndexPath = [IndexPath]() // array of selected indexpath
之后
extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return timeArray.count
}
// -------------------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.view.frame.size.width - 60) / 7, height: (self.view.frame.size.width - 60) / 7)
}
// -------------------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
// dequeue cell
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
// assign text to label
cell.lblTitle.text = self.timeArray[indexPath.item]
if self.arrSelectedIndexPath.contains(indexPath) {// if arrSelectedIndexPath contain indexpath then change backgrond color of label
cell.lblTitle.backgroundColor = UIColor(red: 81.0/255.0, green: 182.0/255.0, blue: 186.0/255.0, alpha: 1.0)
}else { // else set it to default color white
cell.lblTitle.backgroundColor = UIColor.white
}
// return collection view cell
return cell
}
// -------------------------------------------------------------------------------------------
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if self.selectedIndexPath == nil { // if selected indexpath nil then assign first indexpath
self.selectedIndexPath = indexPath
self.arrSelectedIndexPath.removeAll() // remove all items from array
self.collectionView.reloadData() // reload CollectionView
}else {
if self.arrSelectedIndexPath.count == 0 { // if arrSelectedIndexPath.count == 0 then
if self.selectedIndexPath.item < indexPath.item { // check first selected indexpath is smaller then end indexpath
// fill all intermediate cell
for i in self.selectedIndexPath.item...indexPath.item {
self.arrSelectedIndexPath.append(NSIndexPath(item: i, section: 0) as IndexPath)
}
// reload collection view and update background color
self.collectionView.reloadData()
// reset first selected indexpath
self.selectedIndexPath = nil
}else { // else select first selected indexpath
self.selectedIndexPath = indexPath
}
}else { // else remove all items from
self.arrSelectedIndexPath.removeAll()
self.collectionView.reloadData()
self.selectedIndexPath = indexPath
}
}
}
// -------------------------------------------------------------------------------------------
希望对你有用!
关于ios - 在 CollectionView 中自动选择中间单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48275402/