You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击TableView不同行后在CollectionView展示对应数组的实现方法

解决TableView点击切换CollectionView数据源的问题

这问题我熟,其实核心就是让CollectionView的数据源变成动态可变的,然后根据TableView的选中事件切换数据源,最后刷新CollectionView就搞定了。我给你一步步修改代码:

步骤1:添加动态数据源变量

首先在ViewController里新增一个用来承载当前CollectionView数据的变量,代替你现在固定写死的secondArray:

var currentCollectionData: [String] = []

记得在viewDidLoad()里初始化一下默认显示的内容,比如默认显示第一个数组:

override func viewDidLoad() {
    super.viewDidLoad()
    // 初始化默认数据源
    currentCollectionData = firstArray // 你代码里提到了firstArray但没定义,记得补上这个数组的定义
    tableView.delegate = self
    tableView.dataSource = self
    collectionView.delegate = self
    collectionView.dataSource = self
}

(补充:记得加上var firstArray: [String] = ["你的内容"],比如var firstArray: [String] = ["First Item 1", "First Item 2"])

步骤2:修改TableView选中事件的逻辑

在tableView(_:didSelectRowAt:)方法里,根据选中的行索引,给currentCollectionData赋值对应的数组,然后刷新CollectionView:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    switch indexPath.row {
    case 0:
        currentCollectionData = firstArray
    case 1:
        currentCollectionData = secondArray
    case 2:
        currentCollectionData = thirdArray
    default:
        // 处理超出范围的情况,比如默认显示第一个数组
        currentCollectionData = firstArray
    }
    // 刷新CollectionView,让新数据源生效
    collectionView.reloadData()
}

如果你更习惯用if判断,也可以写成这样:

if indexPath.row == 0 {
    currentCollectionData = firstArray
} else if indexPath.row == 1 {
    currentCollectionData = secondArray
} else if indexPath.row == 2 {
    currentCollectionData = thirdArray
} else {
    currentCollectionData = firstArray
}
collectionView.reloadData()

步骤3:修改CollectionView的数据源方法

把CollectionView里用到固定数组的地方,全部换成我们的动态数据源currentCollectionData:

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCell
    cell.collectionLabel.text = currentCollectionData[indexPath.item]
    return cell
}

完整修改后的代码

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UITableViewDelegate, UITableViewDataSource {
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var tableView: UITableView!
    
    var tableArray: [String] = ["1", "2", "3"]
    var firstArray: [String] = ["First Item 1", "First Item 2"]
    var secondArray: [String] = ["One", "Two"]
    var thirdArray: [String] = ["Three", "Four"]
    var fourthArray: [String] = ["Five", "Six"]
    // 新增动态数据源
    var currentCollectionData: [String] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化默认数据源
        currentCollectionData = firstArray
        // 绑定代理和数据源(如果storyboard没拖线的话)
        tableView.delegate = self
        tableView.dataSource = self
        collectionView.delegate = self
        collectionView.dataSource = self
    }
    
    // TABLEVIEW
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! TableCell
        cell.tableLabel.text = tableArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // 根据选中行切换数据源
        switch indexPath.row {
        case 0:
            currentCollectionData = firstArray
        case 1:
            currentCollectionData = secondArray
        case 2:
            currentCollectionData = thirdArray
        default:
            currentCollectionData = firstArray
        }
        // 刷新CollectionView
        collectionView.reloadData()
    }
    
    // COLLECTIONVIEW
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return currentCollectionData.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! CollectionCell
        cell.collectionLabel.text = currentCollectionData[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print(indexPath)
    }
}

class TableCell: UITableViewCell {
    @IBOutlet weak var tableLabel: UILabel!
}

class CollectionCell: UICollectionViewCell {
    @IBOutlet weak var collectionLabel: UILabel!
}

这样修改后,点击TableView的不同行,CollectionView就会自动切换对应的数组内容啦。核心就是用一个可变的数据源来衔接两个控件的交互,然后通过刷新CollectionView让新数据生效。

内容的提问来源于stack exchange,提问作者Dima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:46:11