DZNEmptyDataSet 与 RxSwift 中的表视图绑定不兼容。有人能让它工作吗?

DZNEmptyDataSet not compatible with tableview binding in RxSwift. Was anyone able to make it work?

我正在尝试使用 DZNEmptyDataSet 设置一个空数据集,而我的表视图已绑定到 Rx 变量

let Chats = Variable(Section).

Chats.asObservable()

    .bind(to: tableView.rx.items(dataSource: dataSource))

我的dzn代码如下:

tableView.emptyDataSetSource = self
tableView.emptyDataSetDelegate = self


func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
    let str = "Welcome"
    let attrs = [NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline)]
    return NSAttributedString(string: str, attributes: attrs)
}

func description(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
    let str = "Tap the button below to add your first grokkleglob."
    let attrs = [NSFontAttributeName: UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)]
    return NSAttributedString(string: str, attributes: attrs)
}

问题是,即使 Chats 为空,tableview 的空数据集也不会显示。如果我删除绑定功能,它会显示。我想知道是否有人能够使两者共存?

我把 RxSwiftDZNEmptyDataSet 混在一起了,没问题。 试试下面的代码:

let arrayVariable = Variable([])

arrayVariable
    .asObservable()
    .bindTo(newsTableView.rx.items) { tableView, row, item in
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier", for: IndexPath(row: row, section: 0))
        return cell
    }
    .addDisposableTo(disposeBag)

您必须使用适当的 cell 自定义您的实现,当然还要将一些实际数据分配给 arrayVariable

我遇到了一个问题,如果最后一项被删除,DZNEmptyDataSet 将不会重新出现。我能够通过子类化 RxTableViewSectionedAnimatedDataSource

来让它工作
final class MyRxTableViewSectionedAnimatedDataSource<S: AnimatableSectionModelType>: RxTableViewSectionedAnimatedDataSource<S> {

private var currentItemsCount = 0

var isEmpty: Bool {
    return currentItemsCount == 0
}

override func tableView(_ tableView: UITableView, observedEvent: Event<[S]>) {
    super.tableView(tableView, observedEvent: observedEvent)
    switch observedEvent {
    case let .next(events):
        guard let lastEvent = events.last else { return }
        currentItemsCount = lastEvent.items.count
    default: break
    }
  }
}

然后return dataSource.isEmpty 属性 DZNEmptyDataSetDelegate

func emptyDataSetShouldDisplay(_ scrollView: UIScrollView!) -> Bool {
    return dataSource.isEmpty
}

最初,在委托方法中我 returning number of rows for section 0。当调用 tableView.reloadEmptyDataSet() 时,原始 DataSource 发送已删除事件与 tableView 注册更改之间的延迟使数字保持在零以上。

现在它被自动调用并被赋予正确的 shouldDisplay 值。