Home > Net >  Issue in DiffableDataSource tableview header
Issue in DiffableDataSource tableview header

Time:02-15

I'm using the tableView using DiffableDataSource and only issue which I've faced in header.I'm using viewForheaderSections method for calling header header view appear but on bottom position not top of the list please see the code thanks.

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

CodePudding user response:

According to your code you are not using viewForHeaderInSection. Instead of that you are using viewForFooterInSection. So instead of a header, a footer will appear in the bottom.

If you only need a header change all the methods related to the footer to the header.

  • Related