我在开发过程中遇到了一个错误。最小的目标是iOS 13,您希望使用DiffableDataSource为每个区段提供不同的单元格。我不知道为什么会有错误。我需要你的帮助。
最小目标: iOS 13.0 *
errorCode
线程1:“无法为带有标识符MuseumItemCell的单元格排队列--必须为标识符注册一个nib或类,或者在故事板中连接一个原型单元格”
不要使用Storyboard,只使用编程UI
-✅代码✅--
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")
}
}发布于 2022-09-18 07:46:22
在您的setupTableView方法中设置
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.identifier)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.identifier)然后你再打电话
tableView.dequeueReusableCell(withIdentifier: MuseumBannerCell.id, for: indexPath)尝试使用MuseumBannerCell.id (和MuseumNumberCell.id)注册单元格,因为这是用于去队列的相同标识符。
tableView.register(MuseumBannerCell.self, forCellReuseIdentifier: MuseumBannerCell.id)
tableView.register(MuseumNumberCell.self, forCellReuseIdentifier: MuseumNumberCell.id)https://stackoverflow.com/questions/73760482
复制相似问题