通过从collectionView中选择不同的单元格(这是tableview的标题)来更改tableView单元格 [英] Change tableView Cells by choosing different cells from collectionView which is the Header of tableview

查看:69
本文介绍了通过从collectionView中选择不同的单元格(这是tableview的标题)来更改tableView单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我的表格视图:

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
    }

}

这是我的collectionView:

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


    }

这就是我所说的标题:

override func viewDidLoad() {
    super.viewDidLoad()

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

}

这是我的sortedTimeFrom:

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

推荐答案

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

DeliveryTimeVC 内创建属性:

var dataSource: [Date] = []

override func viewDidLoad() {
    super.viewDidLoad()

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

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

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

最后,在 didSelect 内部执行以下操作:

Finally, inside didSelect do something like this:

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

这篇关于通过从collectionView中选择不同的单元格(这是tableview的标题)来更改tableView单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆