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?