问题描述
我目前在尝试在我的应用程序中开发搜索视图时遇到了障碍。我试图在表格视图单元格中放置一个集合视图,并使用传统的集合视图数据源委托(行单元格等),但是这些实现中的任何一个在现代应用程序中似乎都远非最佳。我们的应用程序使用了“朋友”、“用户”和“场所”三个模型。
我们已经使用 Diffable 数据源和组合布局成功地将我们的一个模型填充到搜索视图中,但是当我尝试添加其他模型时,我收到以下错误:
""*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效参数不满足:部分!= NSNotFound” 以 NSException 类型的未捕获异常终止""
我很想链接设计,但我没有 10 声望
这里是 SearchViewController
import UIKit
fileprivate typealias DataSource = UICollectionViewDiffableDataSource<Section,SearchV2VC.DataType>
fileprivate typealias SourceSnapshot = NSDiffableDataSourceSnapshot<Section,SearchV2VC.DataType>
class SearchV2VC: UIViewController {
var collectionView: UICollectionView! = nil
private var dataSource: DataSource!
var friendData = [FollowingInfo]()
var userData = [FriendInfo]()
var venueData = [Venue]()
override func viewDidLoad() {
super.viewDidLoad()
configureHierarchy()
configureDataSource()
}
private func firstLayoutSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),heightDimension: .fractionalHeight(0.5))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets.bottom = 15
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.9),heightDimension: .fractionalWidth(0.5))
let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize,subitems: [item])
group.contentInsets = .init(top: 0,leading: 15,bottom: 0,trailing: 2)
let section = NSCollectionLayoutSection(group: group)
//section.orthogonalScrollingBehavior = .groupPaging
section.orthogonalScrollingBehavior = .none
return section
}
private func secondLayoutSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),trailing: 2)
let section = NSCollectionLayoutSection(group: group)
//section.orthogonalScrollingBehavior = .groupPaging
section.orthogonalScrollingBehavior = .none
return section
}
private func thirdLayoutSection() -> NSCollectionLayoutSection {
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),heightDimension: .fractionalHeight(2))
let item = NSCollectionLayoutItem(layoutSize: itemSize)
item.contentInsets.bottom = 15
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.35),heightDimension: .fractionalWidth(0.35))
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize,trailing: 0)
let section = NSCollectionLayoutSection(group: group)
navigationItem.title = "You a Bitch"
section.orthogonalScrollingBehavior = .continuous
return section
}
}
extension SearchV2VC {
private func createLayout() -> UICollectionViewCompositionalLayout {
return UICollectionViewCompositionalLayout { (sectionNumber,env) -> NSCollectionLayoutSection? in
switch sectionNumber {
case 0: return self.firstLayoutSection()
case 1: return self.secondLayoutSection()
default: return self.thirdLayoutSection()
}
}
}
}
//MARK: - UICollectionViewDataSource Methods
extension SearchV2VC {
private func configureHierarchy() {
//collectionView.delegate = self
collectionView = UICollectionView(frame: view.bounds,collectionViewLayout: createLayout())
collectionView.delegate = self
collectionView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
collectionView.backgroundColor = .white
collectionView.register(UserCell.self,forCellWithReuseIdentifier: UserCell.reuseIdentifier)
collectionView.register(FriendCell.self,forCellWithReuseIdentifier: FriendCell.reuseIdentifier)
collectionView.register(VenueCell.self,forCellWithReuseIdentifier: VenueCell.reuseIdentifier)
view.addSubview(collectionView)
}
}
extension SearchV2VC {
func configureDataSource(){
dataSource = DataSource(collectionView: collectionView,cellProvider: { (collectionView,index,friend) -> UICollectionViewCell in
switch friend{
case .user(let user):
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UserCell.reuseIdentifier,for: index) as? UserCell else {fatalError("Couldn't Create New Cell")}
cell.viewmodel = UserCellviewmodel(user: user)
return cell
case .friend(let friend):
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: FriendCell.reuseIdentifier,for: index) as? FriendCell else {fatalError("Couldn't Create New Cell")}
cell.viewmodel = FriendCellviewmodel(user: friend)
return cell
case .venue(let venue):
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VenueCell.reuseIdentifier,for: index) as? VenueCell else {fatalError("Couldn't Create New Cell")}
cell.viewmodel = VenueCellviewmodel(venue: venue)
return cell
}
})
dataSource.apply(snapshotForCurrentState(),animatingDifferences: false)
}
func snapshotForCurrentState() -> NSDiffableDataSourceSnapshot<Section,DataType>{
var snapshot = NSDiffableDataSourceSnapshot<Section,DataType>()
snapshot.appendSections(Section.allSections)
let userItems = userData.map { DataType.user($0) }
snapshot.appendItems(userItems,toSection: Section.users)
let friendItems = friendData.map { DataType.friend($0) }
snapshot.appendItems(friendItems,toSection: Section.friends)
let venueItems = venueData.map { DataType.venue($0) }
snapshot.appendItems(venueItems,toSection: Section.venue)
return snapshot
}
}
extension SearchV2VC: UICollectionViewDelegate {
enum DataType: Hashable {
case user(FriendInfo)
case friend(FollowingInfo)
case venue(Venue)
}
func collectionView(_ collectionView: UICollectionView,didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath,animated: true)
guard let user = dataSource.itemIdentifier(for: indexPath) else { return }
print(user)
}
}
这是我们模型的样子:
import Foundation
import Firebase
import MapBox
struct Venue: Hashable {
static func == (lhs: Venue,rhs: Venue) -> Bool {
lhs.venID == rhs.venID
}
func hash(into hasher: inout Hasher) {
hasher.combine(venID)
}
var venID: String?
var latitude: Double?
var longitude: Double?
var name: String?
var description: String?
var image: String?
var address: String?
}
struct FriendInfo: Hashable {
static func == (lhs: FriendInfo,rhs: FriendInfo) -> Bool {
lhs.userID == rhs.userID
}
func hash(into hasher: inout Hasher) {
hasher.combine(userID)
}
var displayName: String?
var userID: String?
var userName: String?
var providerID: String?
var profileImageURL: String?
var isFriend = false
var stats: UserStats?
var isCurrentUser: Bool { return Auth.auth().currentUser?.uid == providerID }
}
struct FollowingInfo: Hashable {
static func == (lhs: FollowingInfo,rhs: FollowingInfo) -> Bool {
lhs.userID == rhs.userID
}
func hash(into hasher: inout Hasher) {
hasher.combine(userID)
}
var displayName: String?
var userID: String?
var profileURL: String?
var userName: String?
}
最后这里是我们正在使用的自定义节类,而不是位于控制器中的通常的“枚举节”,这很可能是问题所在:
import UIKit
struct Section: Hashable {
var id = UUID()
var title: String
var data: [Any]
init(title: String,data: [Any]) {
self.title = title
self.data = data
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: Section,rhs: Section) -> Bool {
lhs.id == rhs.id
}
}
extension Section {
static var allSections: [Section] = [
Section(title: "Users",data: [FriendInfo]()),Section(title: "Friends",data: [FollowingInfo]()),Section(title: "Venue",data: [Venue]())
]
}
extension Section {
static var users: Section =
Section(title: "Users",data: [FriendInfo]())
}
extension Section {
static var friends: Section =
Section(title: "Friends",data: [FollowingInfo]())
}
extension Section {
static var venue: Section =
Section(title: "Venue",data: [Venue]())
}
最后,我应该提一下,我们通过对 Firebase 的 API 调用为这 3 个模型设置了完全不同的数据。 (数据在应用程序的其他任何地方都按预期运行)集合视图单元格正在使用视图模型,因此在视图控制器中看到了“cell.viewmodel = ...”。
几天以来,我一直在努力使其按预期运行。我观看了 Apple 的所有视频,彻底检查了所有示例项目,并访问了谷歌提供的几乎所有关于该主题的链接。
这是我的第一个堆栈问题,如果我错过了任何重要信息,我深表歉意。肯定有人已经实现了这样的设计,我对实现这一目标的任何方式持开放态度。谢谢!
解决方法
我实际上是在尝试为自己解决这个问题时找到了您的帖子,并且刚刚让它起作用。通过使用枚举,您肯定走在正确的轨道上。很抱歉,我无法确切地告诉您该错误的含义,但以下是对我有用的要点。我只有两种不同的模型(事件和过滤器),但显然它可以扩展到 3+。
typealias EventsDataSource = UICollectionViewDiffableDataSource<Section,Item>
typealias EventsSnapshot = NSDiffableDataSourceSnapshot<Section,Item>
struct Filter {
let text: String
}
struct Event {
let description: String
let startTime: Date
let endTime: Date
}
enum Item: Hashable {
case filter(Filter)
case event(Event)
}
struct Section: Hashable {
let items: [Item]
let title: String?
}
Donny Wals 的 Diffable Datasources article 特别好。
(如果您希望将核心数据集成到您的 Diffable 数据源中,Julian Schiavo's article 也非常有用。)