不同对象的UITableViewDiffableDataSource和NSDiffableDataSourceSnapshot仅显示一行 [英] UITableViewDiffableDataSource and NSDiffableDataSourceSnapshot for different objects shows only one row
问题描述
我已经使用 UITableViewDiffableDataSource
和 NSDiffableDataSourceSnapshot
这样的
private typealias ListDataSource = UITableViewDiffableDataSource<Section, Wrapper>
private typealias ListSnapshot = NSDiffableDataSourceSnapshot<Section, Wrapper>
enum Wrapper: Hashable {
case one([Company])
case two([Member])
}
private enum Section: CaseIterable {
case main
}
private func configureDataSource() {
dataSource = ListDataSource(tableView: listTableView,
cellProvider: { [weak self] (_, indexPath, wrapper) -> UITableViewCell? in
guard let `self` = self else {
return UITableViewCell()
}
switch wrapper {
case .one(let company):
let cell = self.listTableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = company[indexPath.row].name
return cell
case .two(let member):
let cell = self.listTableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
cell.textLabel?.text = member[indexPath.row].name.first
return cell
}
})
}
func updateData(_ wrapper: Wrapper) {
var snapshot = ListSnapshot()
snapshot.appendSections([.main])
switch wrapper {
case .one(let comp):
snapshot.appendItems([.one(comp)])
dataSource.apply(snapshot, animatingDifferences: true)
case .two(let member):
snapshot.appendItems([.two(member)])
dataSource.apply(snapshot, animatingDifferences: true)
}
}
有关段更改的信息,更新包装器类型的数据.但是问题只是每次只显示一条记录.
func handleSegmentChanged(_ sender: UISegmentedControl) {
let member = Member(name: Name(first: "Harshal", last: "Wani"),
memberId: "123", age: 30, email: "harshal@gmail.com", phone: "123456789")
let member2 = Member(name: Name(first: "David", last: "John"),
memberId: "123", age: 30, email: "harshal@gmail.com", phone: "123456789")
let comp = Company(name: "Comp 1", companyId: "", website: "", logo: "", about: "", members: [member, member2])
let comp2 = Company(name: "Comp 2", companyId: "", website: "", logo: "", about: "", members: [member, member2])
if sender.selectedSegmentIndex == 0 {
updateData(.one([comp, comp2]))
} else {
updateData(.two(comp.members))
}
}
感谢您的帮助,谢谢
推荐答案
每个部分仅应用一项,则必须声明包装器
You are applying only one item per section, you have to declare the wrapper
enum Wrapper: Hashable {
case one(Company)
case two(Member)
}
在 handleSegmentChanged
中创建由 Wrapper
项目组成的 array 而不是由 one Wrapper
带有相关类型的数组.
In handleSegmentChanged
create an array of Wrapper
items instead of one Wrapper
with an array of associated types.
@IBAction func handleSegmentChanged(_ sender: UISegmentedControl) {
let member = Member(name: Name(first: "Harshal", last: "Wani"), memberId: "123", age: 30, email: "harshal@gmail.com", phone: "123456789")
let member2 = Member(name: Name(first: "David", last: "John"), memberId: "123", age: 30, email: "harshal@gmail.com", phone: "123456789")
let comp = Company(name: "Comp 1", companyId: "", website: "", logo: "", about: "", members: [member, member2])
let comp2 = Company(name: "Comp 2", companyId: "", website: "", logo: "", about: "", members: [member, member2])
if sender.selectedSegmentIndex == 0 {
updateData([.one(comp), .one(comp2)])
} else {
updateData(comp.members.map{.two($0)})
}
}
并将 updateData
替换为
func updateData(_ wrapper: [Wrapper]) {
var snapshot = ListSnapshot()
snapshot.appendSections([.main])
snapshot.appendItems(wrapper)
dataSource.apply(snapshot, animatingDifferences: true)
}
[弱自我]->
跳舞是胡说八道.闭包的第一个参数是表格视图.使用此实例可避免发生 configureDataSource
中的self self
的任何情况,并将 configureDataSource
替换为
The [weak self] -> self
dance in configureDataSource
is nonsense. The first parameter of the closure is the table view. Use this instance to avoid any occurrence of self
and replace configureDataSource
with
private func configureDataSource() {
dataSource = ListDataSource(tableView: listTableView,
cellProvider: { (tableView, indexPath, wrapper) -> UITableViewCell? in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell",
for: indexPath)
switch wrapper {
case .one(let company):
cell.textLabel?.text = company.name
case .two(let member):
cell.textLabel?.text = member.name.first
}
return cell
})
}
这篇关于不同对象的UITableViewDiffableDataSource和NSDiffableDataSourceSnapshot仅显示一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!