0

The problem is I have a Parent TableView and Inside Tableview I have a CollectionView and Inside collection I have another Tableview for each Cell.My Problem is I am not able to load first 0th item of collectionView its starting from 1 and Tableview is loading with 0 index of collection View.

Can anyone Please help me I gave tried so many ways but no luck

This is my Code synppet

extension TennisReservationVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == collectionBallType {
            return 3
        }
        else if collectionView.tag == 1{
           return tennisBookingSettingsModel.tennislocationsData.count
        }
        else{
            return arrDate.count
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if collectionView.tag == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CourtsCollectionViewCell", for: indexPath) as! CourtsCollectionViewCell
            cell.court1TableView.reloadData()
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        if collectionView.tag == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CourtsCollectionViewCell", for: indexPath) as! CourtsCollectionViewCell
            cell.court1TableView.reloadData()
        }
        
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        courtsViewIndex = indexPath.row
        print(courtsViewIndex)
        if collectionView == collectionBallType {
            let cell = collectionBallType.dequeueReusableCell(withReuseIdentifier: "TennisBallTypeCollectionCell", for: indexPath) as! TennisBallTypeCollectionCell
            
            return cell
        }
        else if collectionView.tag == 1 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CourtsCollectionViewCell", for: indexPath) as! CourtsCollectionViewCell
            cell.court1TableView.delegate = self
            cell.court1TableView.dataSource = self
            cell.court1TableView.tag = 2
            return cell
        }
        else {
            let cell = collectionDaySlot.dequeueReusableCell(withReuseIdentifier: "TennisDaySlotCollectionCell", for: indexPath) as! TennisDaySlotCollectionCell
            let dict = tennisBookingSettingsModel.tennisBookingSettings.tennisAvailableDates[indexPath.row]
            cell.lblDay.text = getDayWeek(givenDate: dict.dateSlot ?? "")
            cell.lblMonth.text = getDateGolf(givenDate: dict.dateSlot ?? "")
            if selectedDate == indexPath.row{
                cell.lblMonth.textColor = .white
                cell.lblDay.textColor = .white
                cell.viewBack.layer.backgroundColor = UIColor(red: 1/255, green: 192/255, blue: 247/255, alpha: 1).cgColor
            } else {
                cell.lblDay.textColor = .darkGray
                cell.lblMonth.textColor = .darkGray
                cell.viewBack.layer.backgroundColor = UIColor.white.cgColor
            }
            return cell
        }
    }
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        if collectionView == collectionBallType  {
            
            return CGSize.init(width: 120, height: 75)
            
        }
        else if collectionView.tag == 1{
            let flowayout = collectionViewLayout as? UICollectionViewFlowLayout
            let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0)
            let size:CGFloat = (collectionView.frame.size.width - space) / 2.0
            return CGSize(width: size, height: collectionView.frame.size.height)
        }
        else{
            return CGSize(width: 80, height: 60)
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if collectionView == collectionDaySlot{
            selectedDate = indexPath.row
            let dict = tennisBookingSettingsModel.tennisBookingSettings.tennisAvailableDates[indexPath.row]
            let inputFormatter = DateFormatter()
            inputFormatter.dateFormat = "YYYY-MM-dd hh:mm a"
            inputFormatter.locale = Locale(identifier: NSLocale.current.identifier)
            tennisBookingSettingsModel.SelectedDate = dict.dateSlot ?? ""
            collectionDaySlot.reloadData()
        }
    }
    
}
extension TennisReservationVC : UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView == timeIndexTableView
        {
            return tennisBookingSettingsModel.tennisBookingSettings.tennisTimeIndex.count
        }
        else if tableView == mainTableView
        {
            return 1
        }else {
            return tennisBookingSettingsModel.tennisBookingSettings.tennisTimeIndex.count * 2
        }
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        mainTableViewHeight = CGFloat(tennisBookingSettingsModel.tennisBookingSettings.tennisTimeIndex.count * 4 * 50)

        if  tableView == mainTableView{
            let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",
                                                     for: indexPath) as? MainTableViewCell
            cell?.courtsCollectionView.delegate = self
            cell?.courtsCollectionView.dataSource = self
            cell?.courtsCollectionView.tag = 1
            return cell!
        }
        else if tableView == timeIndexTableView
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "TimeIndexTableViewCell",
                                                     for: indexPath) as? TimeIndexTableViewCell
            let timeIndex = tennisBookingSettingsModel.tennisBookingSettings.tennisTimeIndex[indexPath.row]
            cell?.timeIndexLbl.text = "\(timeIndex.time ?? "") \n" + timeIndex.timePeriod
            return cell!
        }
        else {
            let tennisCourtsData = tennisBookingSettingsModel.tennislocationsData[courtsViewIndex]
            if indexPath.row < tennisCourtsData.tennisLocationsAvailableTimeSlots.count
            {
                let tennisAvailableSlotsData = tennisCourtsData.tennisLocationsAvailableTimeSlots[indexPath.row]
                timeSlotDurationHeight = CGFloat(tennisAvailableSlotsData.timeSlotSize * 50)
                if tennisAvailableSlotsData.timeSlotType == "Reservation" {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "BookedTableViewCell",
                                                             for: indexPath) as? BookedTableViewCell
                    cell?.reserveduserNameLbl.text = tennisAvailableSlotsData.timeSlotHeader
                    cell?.numpersonsLbl.text = String(tennisAvailableSlotsData.membersIncluded)
                    cell?.numguestLbl.text = String(tennisAvailableSlotsData.guestsIncluded)
                    cell?.modificationClickClosure = {
                        if let tennisVC = UIStoryboard.init(name: "TennisStoryboard", bundle: .main).instantiateViewController(withIdentifier: "TennisModifyReservationVC") as? TennisModifyReservationVC {
                            self.navigationController?.pushViewController(tennisVC, animated: true)
                        }
                    }
                    return cell!
                }else if tennisAvailableSlotsData.timeSlotType == "Block"
                {
                    let cell = tableView.dequeueReusableCell(withIdentifier: "MaintanenceTableViewCell",
                                                             for: indexPath) as? MaintanenceTableViewCell
                    cell?.titleLbl.text = tennisAvailableSlotsData.timeSlotDuration
                    cell?.maintenanceDurationLbl.text = tennisAvailableSlotsData.timeSlotHeader
                    return cell!
                }
                else if tennisAvailableSlotsData.timeSlotType == "Event"{
                    let cell = tableView.dequeueReusableCell(withIdentifier: "TennisClinicTableViewCell",
                                                             for: indexPath) as? TennisClinicTableViewCell
                    cell?.titleLbl.text = tennisAvailableSlotsData.timeSlotHeader
                    cell?.durationLbl.text = tennisAvailableSlotsData.timeSlotDuration
                    cell?.availableLbl.text = "Available : " + tennisAvailableSlotsData.available
                    cell?.totalLbl.text = "Total : " + tennisAvailableSlotsData.total
                    cell?.descLbl.text = tennisAvailableSlotsData.eventdescription
                    cell?.clinicClickClosure = {
                        if let tennisVC = UIStoryboard.init(name: "TennisStoryboard", bundle: .main).instantiateViewController(withIdentifier: "TennisClinicVC") as? TennisClinicVC {
                            self.navigationController?.pushViewController(tennisVC, animated: true)
                            
                        }
                    }
                    return cell!
                }
                else {
                    
                    let cell = tableView.dequeueReusableCell(withIdentifier: "OpenTableViewCell",
                                                             for: indexPath) as? OpenTableViewCell
                    cell?.statusBtn.setTitle(tennisAvailableSlotsData.timeSlotType, for: UIControlState.normal)
                    cell?.statusBtn.setTitleColor(hexStringToUIColor(hex: "#4B5154"), for: .normal)
                    cell?.statusClickClosure = {
                        if let tennisVC = UIStoryboard.init(name: "TennisStoryboard", bundle: .main).instantiateViewController(withIdentifier: "TennisSubmitRequestVC") as? TennisSubmitRequestVC {
                            tennisVC.selectedDuration = "90 min"
                            tennisVC.selectedPlayer = 1
                            self.navigationController?.pushViewController(tennisVC, animated: true)
                        }
                    }
                    return cell!
                }
            }
            mainTableView.reloadData()
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView == mainTableView {
            return mainTableViewHeight
        }else if tableView == timeIndexTableView{
            return cellHeight * 2
        }
        else {
            return timeSlotDurationHeight
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if tableView == mainTableView
        {
            return 0
        }else {
            return sectionHeaderHeight
        }
    }
    public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let additionalSeparatorThickness = CGFloat(2)

        let additionalSeparator = UIView(frame: CGRect(x: 0,
                                                       y: cell.frame.size.height - additionalSeparatorThickness, width: cell.frame.size.width, height: additionalSeparatorThickness))
        additionalSeparator.backgroundColor = UIColor.white
        cell.addSubview(additionalSeparator)

    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if tableView == timeIndexTableView
        {
            let cell = tableView.dequeueReusableCell(withIdentifier: "IconTableViewCell") as! IconTableViewCell
            cell.iconImage.image = UIImage(named: "Icon_Calendar")
            return cell

        }else {
//            print(courtsViewIndex)
            let tenniscourtsData = tennisBookingSettingsModel.tennislocationsData[courtsViewIndex]
            let cell = tableView.dequeueReusableCell(withIdentifier: "CourtHeaderTableViewCell") as! CourtHeaderTableViewCell
            cell.iconImg.image = UIImage(named: tenniscourtsData.locationImage)
            cell.titleLbl.text = tenniscourtsData.locationName
            return cell
        }
    }

}

I want my CollectionView to load from 0 index and the tableview in it should load from 0 index

6
  • 1
    First, we don't have enough information to debug this for you - maybe create a minimal reproducible example? Second, without seeing all the information and understanding your design goals, embedding table views like this is generally a bad idea. Third, you should not be calling .dequeueReusableCell(...) inside willDisplayCell or didEndDisplaying cell. What have you done to try and debug this? Have you set breakpoints in your cellForItemAt and cellForRowAt to find out if you're getting the values you're expecting? Commented Sep 8, 2023 at 12:02
  • Yes I have set breakpoint,I am getting values but but reload of tableview is not happening at a first item of collectionView Commented Sep 11, 2023 at 5:28
  • You need to reload your embedded collection / table views after setting the data. The way you have written willDisplayCell and didEndDisplaying cell, you are creating a NEW cell. You might fix the problem by getting rid of the .dequeueReusableCell(...) line and only calling cell.court1TableView.reloadData() ... without being able to run your code, though, it's just a guess. That's why I suggest creating a minimal reproducible example (emphasis on minimal) so we can help you debug it. Commented Sep 11, 2023 at 16:47
  • I have tried it but not working Commented Sep 12, 2023 at 9:44
  • 1
    Working Now I changed the approach Commented Sep 14, 2023 at 6:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.