![](/img/trans.png)
[英]Hello, I am using swift and I am trying to change a label of a collection view cell when the a collection view cell is selected
[英]Trying to update a collection view when tableview cell matches a criteria Swift
我在同一個視圖控制器中有一個表示月份的calendarTableview
和一個表示當前開放時間的30分鍾時間段的timeSlotCollectionView
。 我的意圖是在將視圖控制器加載到calendarTableview
cellForRowAt
我檢查它是否是當前日期並將其設置為選中,並再次加載timeSlotCollectionView
一個標准。 只有當我在物理上選擇行時觸發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()
}
這兩個細胞之間的時間間隔是否可以延遲? 在我的另一個問題中就是這種情況。 非常感謝一如既往。
我發現了問題所在。 我在計算預訂時隙的函數中刷新了timeSlotCollectionView
。 我想,從該函數填充的數組和從該數組中讀取的timeSlotCollectionView
以繪制其單元格之間的時間差距很小,從而產生普通單元格。 我修改了cellForItemAt
所以我可以直接從獲取的結果執行條件檢查,這樣我就可以繞過該函數。 現在選擇calendarTableView
的正確單元格,並使用正確的單元格填充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.cellTime = 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 valueStart in self.fetchedResultController.fetchedObjects! {
// booking start match
if timeStringToStringConvert(cell.cellTime) == timeStringToStringConvert(valueStart.bookingStart!) {//} && cell.cellTime <= timeStringToStringConvert(valueStart.bookingEnd!) {
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: \(valueStart.bookingId!)")
// cell.backgroundColor = UIColor.red.withAlphaComponent(0.8)
cell.bookingState.backgroundColor = UIColor.red.withAlphaComponent(0.8)
cell.backgroundColor = UIColor.red.withAlphaComponent(0.25)
}
else if timeStringToStringConvert(cell.cellTime) > timeStringToStringConvert(valueStart.bookingStart!) && timeStringToStringConvert(cell.cellTime) < timeStringToStringConvert(valueStart.bookingEnd!){
print(" Mid slot found")
print(" mid slot id is: \(String(describing: cell.cellId))")
cell.bookingState.backgroundColor = UIColor.red.withAlphaComponent(0.3)
cell.backgroundColor = UIColor.red.withAlphaComponent(0.15)
cell.isUserInteractionEnabled = false
}
print("bookingId is: \(valueStart.bookingId!)")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.