agastache
agastache

Reputation: 97

How to get tableview [section] outside of tableview methods?

I have a collectionview calendar with a DidSelectMethod and in this method, I'd like to check if the date selected is == to any of the dates in a tableview (the dates are the tableview's Headers) also in the same view controller and then scroll to that date in the tableview.

I'd like some help thinking through the logic for this. Right now I think I need to somehow get the [section] of the tableview outside the tableview methods but I do not think that is the correct way to do this.

Here's what I'm trying so far:

Collection View Calendar's DidSelect method:

func calendar(_ calendar: JTAppleCalendarView, didSelectDate date: Date, cell: JTAppleCell?, cellState: CellState) {
    configureCalendarCell(cell: cell, cellState: cellState)
    cell?.bounce()
    selectedDate = date

// Check if selectedDate is == to any dates in the tableview 

// First, get the dates from TableView's Data Source. 
// The [datesFromCalendarXML] also make up the Headers of each section in TableView
let datesFromCalendarXML = allEventsInVisibleMonth.map {$0.eventdate}

// Reformat selectedDate from collectionView Calendar to String
let selectedDateToString = formatter.string(from: selectedDate!)

// See if the selected date matches any of the tableview dates
if datesFromCalendarXML.contains(selectedDateToString) {

    // set up tableView's scrollToRow
    // get [section] Int of the datesFromCalendarXML that matches
    // Can I get [section] outside of of tableview?
    let sectionOfDate = 

     let indexPath = IndexPath(row: 0 , section: sectionOfDate)
      self.tableView.scrollToRow(at: indexPath, at: UITableView.ScrollPosition.top, animated: true)

} else {}

Looking for a tip on how to go about this.

Upvotes: 1

Views: 121

Answers (1)

agastache
agastache

Reputation: 97

Here's I got this to work, in case someone else is looking.

I used .index(of: _) to get the index of the selectedDate in my allEventsInVisibleMonth array, and then used that index for scrollToRow's indexPath: IndexPath.

let datesFromCalendarXML = allEventsInVisibleMonth.map {$0.eventdate}
    let selectedDateToString = formatter.string(from: selectedDate!)
    if let index = datesFromCalendarXML.index(of: selectedDateToString) {
        let indexPath = IndexPath(row: 0 , section: index)
        self.tableView.scrollToRow(at: indexPath, at:
        UITableView.ScrollPosition.top, animated: true)
    }

Upvotes: 1

Related Questions