IOS 反应器套件电池选择无法正常工作



我正在使用uicollectionViewReactorKit构建共享功能

我的设置是

照片列表视图反应器.class

class PhotoListViewReactor : Reactor {
enum Action {
case shareInit
case select(photo: Photo)
case deselect(photo: Photo)
case shareConfirm
case shareFinish
}
enum Mutation {
case selectShare(_ photo: Photo)
case deselectShare(_ photo: Photo)
case setSharingState(Bool)
case triggerShareAction
case shareComplete
}
struct State {
var sharePhotos: [Photo] = []
var isSharing: Bool = false
var shareAction: Bool = false
}
var initialState = State()
//    init() { }
func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .select(photo: let photo):
return Observable.just(Mutation.selectShare(photo)).takeUntil(self.action.filter(isSharingAction))
case .deselect(photo: let photo):
return Observable.just(Mutation.deselectShare(photo)).takeUntil(self.action.filter(isSharingAction))
case .shareInit:
return Observable.just(Mutation.setSharingState(true))
case .shareConfirm:
return Observable.concat([Observable.just(Mutation.triggerShareAction), Observable.just(Mutation.setSharingState(false))])
case .shareFinish:
return Observable.concat([Observable.just(Mutation.shareComplete),Observable.just(Mutation.setSharingState(false))])
}
}
func reduce(state: State, mutation: Mutation) -> State {
switch mutation {
case let .selectShare(photo):
var newState = state
newState.sharePhotos.append(photo)
return newState
case let .deselectShare(photo):
var newState = state
newState.sharePhotos.removeAll(where: { $0.id == photo.id })
return newState
case let .setSharingState(isSharing):
var newState = state
newState.isSharing = isSharing
return newState
case .triggerShareAction:
var newState = state
newState.shareAction = true
return newState
case .shareComplete:
var newState = state
newState.shareAction = false
newState.isSharing = false
newState.sharePhotos = []
return newState
}
}
private func isSharingAction(_ action: Action) -> Bool {
if case .shareInit = action {
return true
} else {
return false
}
}
}

和内部PhotoListViewController

self.collectionView.rx.modelSelected(Photo.self).share()
.filter(if: reactor.state.map{$0.isSharing})
.map {Reactor.Action.select(photo: $0)}
.bind(to: reactor.action)
.disposed(by: disposeBag)

self.collectionView.rx.modelDeselected(Photo.self).share()
.filter(if: reactor.state.map{$0.isSharing})
.map {Reactor.Action.deselect(photo: $0)}
.bind(to: reactor.action)
.disposed(by: disposeBag)

要清除我的过滤器如果运算符:

extension ObservableType {
/**
Filters the source observable sequence using a trigger observable sequence producing Bool values.
Elements only go through the filter when the trigger has not completed and its last element was true. If either source or trigger error's, then the source errors.
- parameter trigger: Triggering event sequence.
- returns: Filtered observable sequence.
*/
func filter(if trigger: Observable<Bool>) -> Observable<E> {
return self.withLatestFrom(trigger) { ($0, $1) }
.filter { $0.1 }
.map { $0.0 }
}
}

我的问题是选择和取消选择无法正常工作(一旦选择,用户将无法通过再次单击取消选择单元格(。我在 uicollectionview 中启用了多部分。

你必须与UICollectionViewselectItem(at:animated:scrollPosition:)一起使用,并deselectItem(at:animated:)API。 在使用selectItem(at:animated:scrollPosition:)选择索引路径之前,rx.modelDeselect不会发出任何值。

最新更新