为什么要为UITableViewDiffableDataSource cellProvider参数返回nil tableViewCell?

时间:2019-06-11 11:45:42

标签: swift ios13

iOS 13 具有一些用于处理tableView的新 API API 的一个有趣区域>是cell

UITableViewDiffableDataSource提供者参数
    public typealias CellProvider = (UITableView, IndexPath, ItemIdentifierType) -> UITableViewCell?

何时在此处返回一个nil UITableViewCell合适?

1 个答案:

答案 0 :(得分:1)

因此该API仍处于测试阶段,这意味着documentation尚不完整。

它说明:

  

本文档包含有关正在开发的API或技术的初步信息。此信息可能会更改,根据本文档实现的软件应与最终操作系统软件一起进行测试。

TLDR-从现在开始,如果您创建一个UITableView并使用一个返回nil的UITableViewDiffableDataSource,则您的应用将崩溃。

不过,this博客文章讨论了一些新的细节。它并没有提及关于为单元格返回nil的任何内容。

您还可以查看this WWDC会话。在15分钟左右,您可以看到,如果无法创建单元格,则示例代码将引发致命错误。

使用上面的博客,我像这样在Xcode 11中创建了一个简单的tableView

class ViewController: UIViewController {

    enum Section: CaseIterable {
        case friends
        case family
        case coworkers
    }

    struct Contact: Hashable {
        var name: String
        var email: String
    }

    struct ContactList {
        var friends: [Contact]
        var family: [Contact]
        var coworkers: [Contact]
    }

    private let tableView = UITableView()
    private let cellReuseIdentifier = "cell"
    private lazy var dataSource = makeDataSource()

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UITableViewCell.self,
                           forCellReuseIdentifier: cellReuseIdentifier
        )

        tableView.dataSource = dataSource

        view.addSubview(tableView)

        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        tableView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true

        loadData()
    }


    func makeDataSource() -> UITableViewDiffableDataSource<Section, Contact> {
        let reuseIdentifier = cellReuseIdentifier

        return UITableViewDiffableDataSource(
            tableView: tableView,
            cellProvider: {  tableView, indexPath, contact in
                let cell = tableView.dequeueReusableCell(
                    withIdentifier: reuseIdentifier,
                    for: indexPath
                )

                cell.textLabel?.text = contact.name
                cell.detailTextLabel?.text = contact.email
                return cell
            }
        )
    }

    func update(with list: ContactList, animate: Bool = true) {
        let snapshot = NSDiffableDataSourceSnapshot<Section, Contact>()
        snapshot.appendSections(Section.allCases)

        snapshot.appendItems(list.friends, toSection: .friends)
        snapshot.appendItems(list.family, toSection: .family)
        snapshot.appendItems(list.coworkers, toSection: .coworkers)

        dataSource.apply(snapshot, animatingDifferences: animate)
    }

    func loadData() {
        let friends = [
            Contact(name: "Bob", email: "Bob@gmail.com"),
            Contact(name: "Tom", email: "Tom@myspace.com")
        ]

        let family = [
            Contact(name: "Mom", email: "mom@aol.com"),
            Contact(name: "Dad", email: "dad@aol.com")
        ]

        let coworkers = [
            Contact(name: "Mason", email: "tim@something.com"),
            Contact(name: "Tim", email: "mason@something.com")
        ]

        let contactList = ContactList(friends: friends, family: family, coworkers: coworkers)
        update(with: contactList, animate: true)
    }
}

一切正常,所以我决定看看如果为单元格返回nil会发生什么情况,所以我在UITableViewDiffableDataSource中添加了以下代码:

if contact.name == "Bob" {
    return nil
}

最终导致崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource returned a nil cell for row at index path: <NSIndexPath: 0xd6d99b18b93a5a0e> {length = 2, path = 0 - 0}. Table view: <UITableView: 0x7f8d30006200; frame = (-207 -448; 414 896); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x60000239de00>; layer = <CALayer: 0x600002dd0ec0>; contentOffset: {0, 0}; contentSize: {414, 264}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <_TtGC5UIKit29UITableViewDiffableDataSourceOC5iOS1314ViewController7SectionVS2_7Contact_: 0x600002ffc520>>, dataSource: <_TtGC5UIKit29UITableViewDiffableDataSourceOC5iOS1314ViewController7SectionVS2_7Contact_: 0x600002ffc520>' 

事实上,只要数据源应用更新,仅返回nil(根本没有单元格)也会导致崩溃。因此,到目前为止,据我所知,返回nil并不是真正的选择,因为它会导致崩溃。

您可以在github上检出full project