ios - 我想要 collectionView 中的表节标题索引或节标题标题(_ :cellForItemAt:)

标签 ios swift uitableview uicollectionview

我在 UITableViewCell 中实现了一个 Collection View 。我想要 collectionView(_:cellForItemAt:)

中的表格部分标题索引或标题标题

这是我的表格 View 代码:

extension ContentCatVCViewController : UITableViewDataSource {

    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return categories[section]
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return categories.count
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CategoryRow
        print("table row index:: \(indexPath.row)")
        return cell
    }

    func tableView(_ tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
        if let headerTitle = view as? UITableViewHeaderFooterView {
            headerTitle.textLabel?.textColor = UIColor.clear
        }
    }
}

这是我的表格单元格代码:

extension CategoryRow : UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! VideoCell
        cell.layer.borderWidth = 1
        cell.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor

        return cell
    }

}

如何在 collectionView(_:cellForItemAt:) 函数中获取 tableView(_:titleForHeaderInSection:)。请帮忙。

最佳答案

在 cellForRowAt 方法中,您正在加载 CollectionView。意味着您在加载 CollectionView 的位置获取 IndexPath。所以 indexPath.section 将是每个 CollectionView 的标题索引。

关于ios - 我想要 collectionView 中的表节标题索引或节标题标题(_ :cellForItemAt:),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50791225/

相关文章:

swift - Xcode8 : Swift compile errors when my connected iPhone is the destination

javafx.scene.control.TableColumn 不能转换为 javafx.scene.control.TableColumn$CellDataFeatures

uitableview - 静态单元与动态原型(prototype)

ios - 我应该使用哪个 Swift 版本?

android - 智能手机设备的唯一设备标识符

ios - 如何删除通过代码创建的 UIImageView?

ios - 如何在 UIPageViewController 中的两个 ViewController 之间传递数据

ios - 更新 TableView 中的单元格

ios - Swift - TableView 未填充数据但未显示任何错误

ios - viewDidDisappear 后键盘不会消失