Nested Tableview inside CollectionView not loading from first index

41 Views Asked by At

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

0

There are 0 best solutions below