尝试在tableview单元格符合条件时更新集合视图Swift

时间:2019-04-11 17:24:43

标签: ios swift uitableview uicollectionview

我在同一视图控制器中有一个calendarTableview代表一个月,一个timeSlotCollectionView代表当日开放时间,划分为30分钟。我的意图是在将视图控制器加载到calendarTableview cellForRowAt内时,我检查它是否为当前日期并将其设置为选中状态,并再次使用特定条件加载timeSlotCollectionView。仅当我物理上选择该行并触发didSelectRowAt时才可以正常工作,但第一次加载时却没有。所有更新均由我在cellForRowAtdidSelectRowAt中调用的函数完成,但在加载时并未更新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()

        }

创建两个单元格之间是否会有时间间隔?在我的另一个问题中就是这种情况。一如既往的感谢。

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我在计算预订的时隙的函数中刷新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!)")

            }
        }