1

我正在使用使用 DiffableDataSource 的 tableView 并且只有我在 header 中遇到的问题。我正在使用 viewForheaderSections 方法来调用 header 标题视图出现但在底部位置而不是列表顶部请查看代码谢谢。

extension SinlgeTableViewcontroller:UITableViewDelegate {
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: Header.self)) as? Header else {return UIView()}
        cell.lblHeader.text = "Top Mens"
        return cell
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 40
    }
    
    func createDataSource(){
        dataSource = UITableViewDiffableDataSource<ProductsSections,AnyHashable>(tableView: tableView, cellProvider: { tableView, indexPath, itemIdentifier in
            switch self.sectionsData[indexPath.section]{
                case .first:
                    guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: ProductCell.self)) as? ProductCell else {return UITableViewCell()}
                    cell.products = self.products[indexPath.row]
                    return cell
                case .second:
                    guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MensCell.self)) as? MensCell else {return UITableViewCell()}
                    cell.mens = self.mens[indexPath.row]
                    return cell
            }
        })
        
    }
    
    func createSnapshot(){
        var snapshot = NSDiffableDataSourceSnapshot<ProductsSections,AnyHashable>()
        sectionsData.forEach{ $0
            snapshot.appendSections([$0])
        }
      //  snapshot.appendSections([.first,.second])
        snapshot.appendItems(products, toSection: .first)
        snapshot.appendItems(mens, toSection: .second)
        dataSource?.apply(snapshot, animatingDifferences: true, completion: nil)
    }
4

1 回答 1

0

根据您的代码,您没有使用viewForHeaderInSection. 而不是你正在使用的viewForFooterInSection. 所以不是 a header, afooter将出现在底部。

如果您只需要header更改所有footerheader.

于 2022-02-15T08:08:34.987 回答