ios – The way to name/set delegate strategies when utilizing UITableViewDiffableDataSource

0
10


I’ve this code:

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

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

        skillsTableView = UITableView(body: .zero, type: .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()
    }


    non-public 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
    }
    
    non-public func resetSnapshot(){
        var snapshot = skillsDataSource.snapshot()
        snapshot.deleteAllItems()
        snapshot.appendSections([.main])
        let addSkillViewItem = SkillViewItem(order: 0, skillId: nil, skillName: nil, thumbnailFileName: "take a look at", latestUpdate: nil)
        snapshot.appendItems([addSkillViewItem], toSection: .fundamental)
        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.mix(skillId)
    }
}

enum SkillsInGroupTableSection {
    case fundamental
}

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

    // ... Different delegate Strategies... // 
}

Proper now the desk view exhibits up with a single cell, however the delegate strategies in heightForHeaderInSection, viewForHeaderInSection are by no means fired/executed. I consider you often do one thing like tableView.delegate = self, after which the delegate strategies outlined on the viewController class are referred to as.

Do I nonetheless want to do that when utilizing a diffable knowledge supply? How do I activate these?

LEAVE A REPLY

Please enter your comment!
Please enter your name here