0

我已经使用和喜欢实现了 UITableViewUITableViewDiffableDataSourceNSDiffableDataSourceSnapshot

    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)
        }
    }

在段更改时,更新 Wrapper 类型的数据。但问题是每次只显示一条记录。

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))
    }
}

感谢任何帮助,谢谢

4

1 回答 1

2

您每个部分仅应用一项,您必须声明包装器

enum Wrapper: Hashable {
    case one(Company)
    case two(Member)
}

handleSegmentChanged创建一个项目数组Wrapper而不是一个 Wrapper具有关联类型数组的项目。

@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)
}

里面的[weak self] -> self舞蹈configureDataSource是胡说八道。闭包的第一个参数是表格视图。使用此实例来避免任何出现self并替换configureDataSource

 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
    })
}
于 2020-07-19T07:44:14.943 回答