通过从 collectionView 中选择不同的单元格来更改 tableView 单元格,这是 tableview 的 Header

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

我将 collectionView 作为我的 tableview header。现在我需要根据我从 collection 视图中选择的单元格更改 tableView 的单元格。我如何从 collection 视图的 didselect 中执行此操作?

这是我的表格视图:

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
    }

}

这是我的 collection视图:

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


    }

这就是我对 header 的称呼:

override func viewDidLoad() {
    super.viewDidLoad()

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

}

这是我的 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: [=14=], 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
}()

免责声明:在浏览器中输入,未在 Xcode 中测试。

DeliveryTimeVC 中创建一个 属性:

var dataSource: [Date] = []

override func viewDidLoad() {
    super.viewDidLoad()

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

然后,在您的 tableView 数据源方法中访问该值:

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
    }
}

最后,在 didSelect 里面做这样的事情:

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