iOS Development

swift – the right way to resolve DiffableDataSource mulipleCell crash (iOS 13)


I encountered an error throughout growth.
The minimal goal is iOS 13.
You need to give totally different cells for every part utilizing DiffableDataSource.
I do not know why there’s an error.
I would like your assist.

minumum goal: iOS 13.0 *

🚨 errorCode 🚨

Thread 1: “unable to dequeue a cell with identifier MuseumItemCell – should register a nib or a category for the identifier or join a prototype cell in a storyboard”

do not used Storyboard, solely programming UI

— ✅ CODE ✅ —


import UIKit
import SnapKit

remaining class MuseumViewController: UIViewController {
    
    enum Merchandise: Hashable {
        case bannerItem([String])
        case numberItem([Int])
    }
    
    enum MySection: Int {
        case banner
        case quantity
    }
    
    
    var tableView = UITableView()
    var dataSource: UITableViewDiffableDataSource<MySection, Merchandise>!
    var banners: [String] {
        [
            "ABCDEFGHIJKL",
            "ABCDE",
            "A",
        ]
    }
    var numbers: [Int] = [100000, 10000, 1000, 100, 10, 10]
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        setupTableView()
        
        fetchDataSource()
        fetchSnapShot()
    }
    
    func setupTableView() {
        view.addSubview(tableView)
        
        tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.identifier)
        tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.identifier)
        
        tableView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }
    }
    
    func fetchSnapShot() {
        var snapshot = NSDiffableDataSourceSnapshot<MySection, Merchandise>()
        snapshot.appendSections([.banner, .number])
        snapshot.appendItems([.bannerItem(banners), .numberItem(numbers)])
        dataSource.apply(snapshot)
    }
    
    func fetchDataSource() {
        dataSource = UITableViewDiffableDataSource<MySection, Merchandise>(
            tableView: tableView
        ) { tableView, indexPath, itemIdentifier in
            
            let part = MySection(rawValue: indexPath.part)
            
            change part {
            case .banner:
                guard let cell = tableView.dequeueReusableCell(
                    withIdentifier: MuseumBannerCell.id,
                    for: indexPath
                ) as? MuseumBannerCell else { return UITableViewCell() }
                
                cell.backgroundColor = .purple
                
                return cell
                
            case .quantity:
                guard let cell = tableView.dequeueReusableCell(
                    withIdentifier: MuseumNumberCell.id,
                    for: indexPath
                ) as? MuseumNumberCell else { return UITableViewCell() }
                
                cell.backgroundColor = .blue
                
                return cell
            default:
                return UITableViewCell()
            }
        }
        
    }
}


class MuseumBannerCell: UITableViewCell {
    static let id: String = "MuseumBannerCell"
    
    // MARK: - Initialize
    override init(type: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(type: type, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
}


class MuseumNumberCell: UITableViewCell {
    static let id: String = "MuseumItemCell"
    
    // MARK: - Initialize
    override init(type: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        tremendous.init(type: type, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been carried out")
    }
}

What's your reaction?

Leave A Reply

Your email address will not be published.