Change tableView Cells by choosing different cells from collectionView which is the Header of tableview

浪子不回头ぞ 提交于 2019-12-24 07:36:24

问题


I have the collectionView as my tableview header. Now I need to change the cells of the tableView depending on the cell I choose from the collection view. How do I do this from didselect of the collectionview?

This is my tableview:

extension DeliveryTimeVC: UITableViewDelegate, UITableViewDataSource {

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sortedTimeFrom.count
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

//        print("Section \(indexPath.section), Row : \(indexPath.row)")
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.DeliveryTimeCell, for: indexPath) as! DeliveryTimeCell

        cell.configureCell(timeFrom: sortedTimeFrom[indexPath.row], timeTo: sortedTimeTo[indexPath.row])

        return cell
    }

}

And this is my collectionView:

extension DeliveryTimeVC : UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return sortedDates.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.DeliveryDateCell, for: indexPath) as! DeliveryDateCell

        cell.configureCell(date: sortedDates[indexPath.row], day: sortedDay[indexPath.row])

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Identifiers.DeliveryDateCell, for: indexPath) as! DeliveryDateCell


    }

and this is how I call the header:

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.tableHeaderView = collectionView
    tableView.tableFooterView = UIView()

}

here is my sortedTimeFrom:

var sortedTimeFrom: [Date] = {


    let today = Date()
    var sortedTime: [Date] = []
    var calender = Calendar(identifier: .iso8601)
    calender.locale = Locale(identifier: "en_US_POSIX")
    let currentHour = calender.component(.hour, from: today)

    (0...(24-currentHour)).forEach {

        guard let newDate = calender.date(byAdding: .hour, value: $0, to: today),
            calender.component(.hour, from: newDate) >= 10
                && calender.component(.hour, from: newDate) <= 22
                && calender.component(.hour, from: newDate) != 0
                && calender.component(.hour, from: newDate) >= (currentHour+3)  else {
                return
        }
        //convert date into desired format
        sortedTime.append(newDate)
    }


    return sortedTime
}()

回答1:


Disclaimer: typed in browser, not tested in Xcode.

Create a property inside DeliveryTimeVC:

var dataSource: [Date] = []

override func viewDidLoad() {
    super.viewDidLoad()

    dataSource = sortedTimeFrom
    ///... other stuff
}

Then, inside your tableView dataSource methods access that value:

extension DeliveryTimeVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: Identifiers.DeliveryTimeCell, for: indexPath) as! DeliveryTimeCell

        cell.configureCell(timeFrom: dataSource[indexPath.row], timeTo: sortedTimeTo[indexPath.row] 

        return cell
    }
}

Finally, inside didSelect do something like this:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
   dataSource = sortedTimeAgain
   tableView.reloadData()
}


来源:https://stackoverflow.com/questions/59255664/change-tableview-cells-by-choosing-different-cells-from-collectionview-which-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!