我有在同一视图控制器calendarTableview
表示月份和timeSlotCollectionView
表示当前日期打开时间在30个分钟内槽划分。 我的意图是,在里面装的视图控制器calendarTableview
cellForRowAt
我检查它是否是当前日期,并将其设置为选择,并加载timeSlotCollectionView
与再次一定的标准。 的所有作品,ES预计只有当我实际选择该行,触发didSelectRowAt
但不是第一次加载。 所有更新由我都调用一个函数来完成cellForRowAt
和didSelectRowAt
,但负载没有更新timeSlotCollectionView
。 我曾在我的应用程序,当你真正选择一个时间段,并与我刚才的问题已经解决了下一阶段的类似的问题,但我不能适用,在这种情况下。 你能看到我弄错了?
功能如下:
对于calendarTableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "calendarCell", for: indexPath) as! CalendarTableViewCell
// Configure the cell...
let date = datesArray[indexPath.row]
print(date)
let calendar = Calendar.current
let components = calendar.dateComponents([.year, .month, .day, .weekday], from: date)
cell.dayLabel.text = "\(String(describing: components.day!))" + " " + "\(dayNamesArray[components.weekday! - 1])"
cell.cellWeekday = components.weekday!
print("cell weekday is: \(cell.cellWeekday!)") // prints correct weekday
cell.cellId = "\(String(format:"%04d", components.year!))" + "\(String(format:"%02d", components.month!))" + "\(String(format:"%02d", components.day!))"
self.selectedDate = cell.cellId // used for time slots cellId
// highlighting current day cell
if indexPath.row == self.actualDay - 1 && self.actualMonth == self.displayedMonth {
cell.dayLabel.backgroundColor = UIColor.red.withAlphaComponent(0.3)
// emulate user selecting the cell
tableView.selectRow(at: indexPath, animated: false, scrollPosition: UITableViewScrollPosition.none) // changing to .middle makes the tableview go looping
print(" @@@@@@@@@@@@@@@@@@@@@@@@@@ selected cell weekday is: \(cell.cellWeekday!) @@@@@@@@@@@@@@@@@@@@ ")
self.updateTimeSlots(selectedCell: cell)
// self.actualWeekday = cell.cellWeekday!
// self.selectedDate = cell.cellId
// calculateOpenTimeSlots()
}
let cornerRadius: CGFloat = 5
cell.layer.cornerRadius = cornerRadius
cell.clipsToBounds = true
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as! CalendarTableViewCell
self.updateTimeSlots(selectedCell: cell)
// self.actualWeekday = cell.cellWeekday!
// self.selectedDate = cell.cellId
// print(" selected cell weekday is: \(cell.cellWeekday!)")
// calculateOpenTimeSlots()
}
对于timeSlotCollectionView
:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeSlotCell", for: indexPath) as! TimeSlotCollectionViewCell
// let booking = self.fetchedResultController.object(at: indexPath)
// Configure the cell
cell.timeLabel.text = timeSlotArray[indexPath.row]
cell.cellId = Int64("\(String(describing: self.selectedDate))" + self.timeStringToStringConvert(timeSlotArray[indexPath.row]))!
// method two USING fetchResultController
if (self.fetchedResultController.fetchedObjects?.count)! > 0 {
for value in self.fetchedResultController.fetchedObjects! {
if Int64(value.bookingId!) == cell.cellId {
print(" match found")
print("Index is: \(index)")
print("cell time is: \(timeSlotArray[indexPath.row])")
print("time slot cell id is: \(String(describing: cell.cellId))")
print("booking id: \(bookingId)")
// cell.backgroundColor = UIColor.red.withAlphaComponent(0.8)
cell.bookingState.backgroundColor = UIColor.red.withAlphaComponent(0.8)
}
}
}
print(" cell.cellId is : \(String(describing: cell.cellId!))")
print(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ time slot created @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ")
// Method one : USING ARRAY works in realtime
// if bookedTimeSlotsArray.count > 0 {
// for index in 0...bookedTimeSlotsArray.count - 1 {
// let bookingId = bookedTimeSlotsArray[index].bookingId
//
// if cell.cellId == bookingId {
// print(" match found")
// print("Index is: \(index)")
// print("cell time is: \(timeSlotArray[indexPath.row])")
// print("time slot cell id is: \(String(describing: cell.cellId))")
// print("booking id: \(bookingId)")
//// cell.backgroundColor = UIColor.red.withAlphaComponent(0.8)
// cell.bookingState.backgroundColor = UIColor.red.withAlphaComponent(0.8)
// }
// }
// }
return cell
}
和值来更新:
func updateTimeSlots(selectedCell: CalendarTableViewCell) {
self.actualWeekday = selectedCell.cellWeekday!
self.selectedDate = selectedCell.cellId
print(" selected cell weekday is: \(selectedCell.cellWeekday!)")
fetchBookings()
calculateOpenTimeSlots()
}
它可以是细胞创建两个之间的时间差? 在我的其他问题是这种情况。 非常感谢一如既往。