- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前在尝试在我的应用程序中开发搜索 View 时碰壁了。我尝试将 Collection View 放置在 TableView 单元格中,并使用传统的 Collection View 数据源委托(delegate)(行单元格等),但是这些实现中的任何一个在现代应用程序中似乎都远非最佳。我们的应用程序为“ friend ”、“用户”和“地点”使用了三种模型。
我们已经成功地使用 Diffable Data Source 和组合布局将其中一个模型填充到搜索 View 中,但是当我尝试添加其他模型时,出现以下错误:
""*** 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无效参数不满足:部分!= NSNotFound'以 NSException""类型的未捕获异常终止
这是 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), 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 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, subitems: [item])
group.contentInsets = .init(top: 0, leading: 15, bottom: 0, 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?
}
最后这里是我们正在使用的自定义部分类,而不是 Controller 中通常的“枚举部分”,这很可能是问题所在:
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 个模型的数据完全不同。 (数据在应用程序的其他任何地方都按预期运行) Collection View 单元格正在使用 View 模型,因此在 View Controller 中看到“cell.viewModel = ...”。
几天来,我一直在努力使它按预期运行。我观看了 Apple 的每个视频,彻底检查了所有示例项目,并访问了 google 就该主题提供的几乎每个链接。
这是我的第一个堆栈问题,所以如果我错过了任何重要信息,我深表歉意。当然有人已经实现了类似这种设计的东西,我愿意接受任何实现它的方法。谢谢!
最佳答案
实际上,我在尝试自己解决这个问题时发现了您的帖子,并且让它发挥了作用。通过使用枚举,您绝对是在正确的轨道上。很抱歉,我无法确切地告诉您该错误的含义,但这是对我有用的要点。我只有两个不同的模型(事件和过滤器),但显然它可以扩展到 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 .
(如果您希望将 Core Data 集成到您的 Diffable 数据源中,Julian Schiavo's article 也非常有帮助。)
关于ios - 如何在可区分数据源中使用多个数据模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68356647/
我试图通过预准备语句使用同一连接执行多个查询,但无法完全实现! 代码片段: public class PostPrReqDaoImpl implements PostPrReqDaoInterface
我目前有一个 2 列宽的 DataGridView,第一列是 DataGridViewTextBoxColumn,第二列是 DataGridViewComboBoxColumn。我还有一个预生成的通用
当我在一台机器上运行以下代码时,我得到了 org.apache.tomcat.dbcp.dbcp.BasicDataSource 的 tomcat 实现,当我在另一台机器上运行它时,我得到了 org.
不确定这是否可行,但这是我的设置。 我有一台带有双启动功能的笔记本电脑。 一个一个分区我有 WinXP 和 MSAccess 2000在另一个分区上,Ubuntu 10.04,带有 apache we
我试过: czmlDataSource.load(czmlurl).then(function(){ viewer.dataSource
我有一个 TableView 和一个数组源。当我在 viewDidLoad 方法中初始化数组时,tableview 显示数组中的数据。当我从 Internet 上的 XML 数据的 URL 填充数组时
我对 DataSource 和 SessionFactory 之间的区别感到困惑。 我认为SessionFactory是一个用于检索 session 的管理器(我猜这实际上是与数据库的连接)。 Dat
我想存储大量(~数千)个字符串并能够使用通配符执行匹配。 例如,这里是一个示例内容: Folder1 文件夹 1/Folder2 Folder1/* Folder1/Folder2/Folder3 文
我有一个 DataGridView 和一个从 SQL 表填充的一些对象的列表。我曾使用两种方法将列表绑定(bind)到网格。 1.直接使用列表到数据源 grdSomeList.DataSource =
我正在尝试在 DataGridView 中设置一些内容。看起来这应该很简单,但我遇到了麻烦。我想显示三列: 代码ID 代号 带有 TypeName 的 DisplayMember 和 TypeID 的
在我的 Config.groovy我把线: grails.config.locations = [ "classpath:app-config.properties"] 我在哪里设置数据源的定义。文件
为了这个问题,假设我有一个包含各种酒类的 Excel 数据源电子表格。 (Cell A) | (Cell B) Bacardi | Rum Smirnoff | Vodka Another Vodka
由于我经常使用第三方 API,我认为创建一些 Magento 模块以实现轻松连接和查询它们会很有帮助。理想情况下,您可以像这样查询 API... $data = Mage::getModel( 'to
将后台线程频繁更新的数据源与 GUI 主线程同步的最佳方法是什么? 我应该在每个方法调用周围放置一个 pthread 互斥体吗?这对我来说似乎也很重。 编辑:我正在寻找 10.5 解决方案 最佳答案
经过几个小时的点击和试用,在查看各种帖子寻求帮助后,这段代码终于起作用了。但我希望有人帮助我理解函数(i,dat),这意味着什么?下面是我的完整代码 - function get_assignedta
我使用的是 Wildfly 10.1 版本,有两个数据源,如下所示, jdbc:mysql://${dbhostn
我正在学习数据源,我想我开始理解它,但我不明白这一段。 据我所知,MySQL 和 PostgreSQL 等数据库供应商编写了自己的不同 DataSource 接口(interface)的实现。现在,这
我有一个关于 TomEE 和使用 tomee.xml 中指定的数据源的奇怪问题。值得注意的是,我使用的是 Netbeans、TomEE 和 MySQL。在 Ubuntu 13.04(Xubuntu 最
WWDC 2019 确实充满了 iOS 的新内容以及 TableViews 和 CollectionView 的新数据源,即 UITableViewDiffableDataSource . 我已成功将
我在独立模式下运行 jboss 并将 standalone.xml 中的数据源设置为以下内容: jdbc:sqlserver://myip:1433;databaseNam
我是一名优秀的程序员,十分优秀!