0

I have this code:

class CreateSkillGroupViewController: UIViewController {
    
    var skillsDataSource: SkillsInGroupDataSource! = nil
    var skillsTableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureNavigationItem()

        skillsTableView = UITableView(frame: .zero, style: .insetGrouped)
        skillsTableView.register(SkillSummaryCell.self)

        view.addSubview(skillsTableView)
        skillsTableView.snp.makeConstraints{ (make) in
            make.edges.equalToSuperview()
        }
        skillsTableView.setEditing(true, animated: false)
        configureDataSource()
        resetSnapshot()
        skillsTableView.reloadData()
    }


    private func configureDataSource() {
        skillsDataSource = SkillsInGroupDataSource(tableView: skillsTableView) { (tableView, indexPath, skillViewItem) -> UITableViewCell? in
            let cell = tableView.reuse(SkillSummaryCell.self, indexPath)
            cell.configure(with: skillViewItem)
            return cell
        }
        skillsTableView.dataSource = skillsDataSource
    }
    
    private func resetSnapshot(){
        var snapshot = skillsDataSource.snapshot()
        snapshot.deleteAllItems()
        snapshot.appendSections([.main])
        let addSkillViewItem = SkillViewItem(order: 0, skillId: nil, skillName: nil, thumbnailFileName: "test", latestUpdate: nil)
        snapshot.appendItems([addSkillViewItem], toSection: .main)
        skillsDataSource.apply(snapshot,animatingDifferences: false,completion: nil)
    }

}

struct SkillViewItem {
    var order: Int?
    var skillId: Int64?
    var skillName: String?
    var thumbnailFileName: String?
    var latestUpdate: Date?
}

extension SkillViewItem: Hashable {
    static func == (lhs: SkillViewItem, rhs: SkillViewItem) -> Bool {
        return lhs.skillId == rhs.skillId
    }
    func hash(into hasher: inout Hasher) {
        hasher.combine(skillId)
    }
}

enum SkillsInGroupTableSection {
    case main
}

class SkillsInGroupDataSource: UITableViewDiffableDataSource<SkillsInGroupTableSection, SkillViewItem> {
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 100
    }
    func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let image = UIImageView(frame: .zero)
        image.translatesAutoresizingMaskIntoConstraints = false
        image.snp.makeConstraints{ (make) in
            make.width.equalTo(300)
            make.height.equalTo(300)
        }
        image.image = UIImage(named: "AppIcon-bw")
        return image
    }

    // ... Other delegate Methods... // 
}

Right now the table view shows up with a single cell, but the delegate methods in heightForHeaderInSection, viewForHeaderInSection are never fired/executed. I believe you usually do something like tableView.delegate = self, and then the delegate methods defined on the viewController class are called.

Do I still need to do this when using a diffable data source? How do I activate these?

BigBoy1337
  • 4,735
  • 16
  • 70
  • 138
  • 1
    Yes, you still need to set the delegate if you want delegate methods to be called. The delegate and the data source are different things. – Paulw11 Aug 02 '22 at 22:51
  • @Paulw11 I noticed that some delegate methods like `func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool ` do get called in the data source subclass whereas others like the viewForHeaderInSection or heightForHeaderInSection only started working when I set my viewController to be the delegate of the tableView. Are some of these data source delegate methods and others table view delegate methods? – BigBoy1337 Aug 02 '22 at 23:12
  • 1
    Yes `canMoveRowAt` is part of the [`UITableViewDataSource`](https://developer.apple.com/documentation/uikit/uitableviewdatasource) protocol – Paulw11 Aug 03 '22 at 05:55

0 Answers0