2017-04-26 42 views
1

我試圖建立一個空的數據集DZNEmptyDataSet,而我的tableview勢必給RX變量DZNEmptyDataSet與RxSwift中的tableview綁定不兼容。有沒有人能夠使它工作?

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的空數據集不會顯示。如果我刪除綁定功能,它會顯示。我想知道是否有人能夠讓這兩個人共存?

回答

1

我有一個問題,如果最後一項被刪除,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 
    } 
    } 
} 

然後在DZNEmptyDataSetDelegate

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

最初返回dataSource.isEmpty財產,在委託方法我返回number of rows for section 0。原始DataSource發送已刪除事件時以及當tableView註冊更改時,之間的延遲將在調用tableView.reloadEmptyDataSet()時將該數字保持爲零。

現在它被自動調用並被賦予正確的shouldDisplay值。

0

我混合RxSwiftDZNEmptyDataSet,這很好。下面 try代碼:

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) 

您必須自定義您的實現與適當細胞當然分配一些實際數據arrayVariable

+0

是的,它沒有工作時綁定到rxdatasource表。我猜它的rxdatasource與它不兼容。我不得不在桌子上放置一個可觀察的東西,如果它叫做refreshemptydataset。 – Ryan

相關問題