通过从tableView的标头collectionView中选择不同的单元格来更改tableView单元格

时间:2019-12-09 19:50:36

标签: swift iphone uitableview uicollectionview tableview

我将collectionView作为tableview标头。现在,我需要根据从集合视图中选择的单元格来更改tableView的单元格。如何从collectionview的didselect中执行此操作?

enter image description here

这是我的表格视图:

gcloud compute operations describe OPERATION_ID --zone

这是我的collectionView:

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
    }

}

这就是我所说的标题:

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


    }

这是我的sortedTimeFrom:

override func viewDidLoad() {
    super.viewDidLoad()

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

}

1 个答案:

答案 0 :(得分:1)

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

DeliveryTimeVC内创建一个属性:

var dataSource: [Date] = []

override func viewDidLoad() {
    super.viewDidLoad()

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

然后,在tableView dataSource方法内部访问该值:

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