首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决DiffableDataSource mulipleCell崩溃(iOS 13)

如何解决DiffableDataSource mulipleCell崩溃(iOS 13)
EN

Stack Overflow用户
提问于 2022-09-18 06:09:04
回答 1查看 24关注 0票数 1

我在开发过程中遇到了一个错误。最小的目标是iOS 13,您希望使用DiffableDataSource为每个区段提供不同的单元格。我不知道为什么会有错误。我需要你的帮助。

最小目标: iOS 13.0 *

errorCode

线程1:“无法为带有标识符MuseumItemCell的单元格排队列--必须为标识符注册一个nib或类,或者在故事板中连接一个原型单元格”

不要使用Storyboard,只使用编程UI

-✅代码✅--

代码语言:javascript
复制
import UIKit
import SnapKit

final class MuseumViewController: UIViewController {
    
    enum Item: Hashable {
        case bannerItem([String])
        case numberItem([Int])
    }
    
    enum MySection: Int {
        case banner
        case number
    }
    
    
    var tableView = UITableView()
    var dataSource: UITableViewDiffableDataSource<MySection, Item>!
    var banners: [String] {
        [
            "ABCDEFGHIJKL",
            "ABCDE",
            "A",
        ]
    }
    var numbers: [Int] = [100000, 10000, 1000, 100, 10, 10]
    
    override func viewDidLoad() {
        super.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, Item>()
        snapshot.appendSections([.banner, .number])
        snapshot.appendItems([.bannerItem(banners), .numberItem(numbers)])
        dataSource.apply(snapshot)
    }
    
    func fetchDataSource() {
        dataSource = UITableViewDiffableDataSource<MySection, Item>(
            tableView: tableView
        ) { tableView, indexPath, itemIdentifier in
            
            let section = MySection(rawValue: indexPath.section)
            
            switch section {
            case .banner:
                guard let cell = tableView.dequeueReusableCell(
                    withIdentifier: MuseumBannerCell.id,
                    for: indexPath
                ) as? MuseumBannerCell else { return UITableViewCell() }
                
                cell.backgroundColor = .red
                
                return cell
                
            case .number:
                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(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


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

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-18 07:46:22

在您的setupTableView方法中设置

代码语言:javascript
复制
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.identifier)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.identifier)

然后你再打电话

代码语言:javascript
复制
tableView.dequeueReusableCell(withIdentifier: MuseumBannerCell.id, for: indexPath)

尝试使用MuseumBannerCell.id (和MuseumNumberCell.id)注册单元格,因为这是用于去队列的相同标识符。

代码语言:javascript
复制
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.id)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.id)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73760482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档