cuốn sách gpt4 ai đã làm

swift - 如何在 RxDataSource 中反射(reflect)新的部分到 UICollectionView?

In lại Tác giả: Walker 123 更新时间:2023-11-28 05:46:41 29 4
mua khóa gpt4 Nike

我正在使用 VIPER 架构使用 RxSwift 和 RxDataSource 构建一个 iOS 应用程序。我想在演示者的值发生变化时更改 UICollectionView 的内容(当用户在搜索栏中输入一些字母时,collectionView 应该显示所有以字母开头的用户个人资料),但它不起作用我想要它。

通过尝试 debug() 函数,ViewController 中 presenter.section 的值(保存 CollectionView 的内容)在我输入一些字母后发生了变化搜索栏。但是,collectionView 没有反射(reflect)出变化。以下是代码的主要部分。

ViewController代码

override func viewDidLoad() {

super.viewDidLoad()
self.view.backgroundColor = .white

self.presenter.section
.drive(self.searchedFriendsCollectionView.rx.items(dataSource: self.dataSource))
.disposed(by: self.disposeBag)

self.searchedFriendsCollectoinView.rx
.setDelegate(self)
.disposed(by: self.disposeBag)
}

Presenter代码

init(view: SearchFriendsViewInterface, interactor: 
SearchFriendsInteractorInterface, wireframe:
SearchFriendsWireframeInterface) {

self.view = view
self.interactor = interactor
self.wireframe = wireframe

let allUsers = self.interactor.allUsers().asObservable()

self.view.searchText
.debounce(0.5)
.filterNil()
.distinctUntilChanged()
.filterEmpty()
.asObservable()
.flatMap { text -> Observable<[SearchFriendsSection]> in
// get all users starting with "text"
allUsers.mapSections(query: text)
}
.bind(to: self.section)
.disposed(by: self.disposeBag)
}

extension Observable where E == [User] {

fileprivate func mapSections(query: String) -> Observable<[SearchFriendsSection]> {

return self.map {
$0.filter { $0.userDisplayName.hasPrefix(query) || $0.username.hasPrefix(query) }
}
.map { users -> [SearchFriendsSection] in

let items = users.map { user in
SearchFriendsItem(icon: user.profileImageURL, displayName: user.userDisplayName)
}
return [SearchFriendsSection(items: items)]
}
}
}

我如何定义nguồn dữ liệu

override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {

self.searchedFriendsCollectoinView = UICollectionView(frame: .init(), collectionViewLayout: self.searchedFriendsCollectionViewFlowLayout)
let dataSource = RxCollectionViewSectionedReloadDataSource (configureCell: {(_, collectionView, indexPath, item) -> UICollectionViewCell in
collectionView.register(SearchFriendsCell.self, forCellWithReuseIdentifier: "SearchFriendsCell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SearchFriendsCell", for: indexPath) as! SearchFriendsCell
cell.item = item
return cell
})
self.dataSource = dataSource
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
  • ViewController 有 Presenter 的实例,presenter 有一个名为 phần 的实例。此phần 包含用户名以特定字母开头的用户列表。

你能帮帮我吗?如果我有任何不清楚的地方,请在评论中告诉我。

gia hạn:debug()

的输出
2019-02-01 10:22:27.610: values -> subscribed
2019-02-01 10:22:27.611: values -> Event next([])
--after typed in some letters in the searchBar--
2019-02-01 10:22:41.494: values -> Event
next([AppScreen.SearchFriendsSection(items:
[AppScreen.SearchFriendsItem(....),
AppScreen.SearchFriendsItem(....),
AppScreen.SearchFriendsItem(....)])])

câu trả lời hay nhất

我发现了你的问题。您从未将 Collection View 添加到主视图中。如果 Collection View 没有 super View ,或者它的大小为零,则它不会自行加载。

将这两行放在您的 viewDidLoad 中:

searchedFriendsCollectionView.frame = view.bounds
view.addSubview(searchedFriendsCollectionView)

当你这样做时,你应该将这一行从你的配置闭包移动到 viewDidLoad 中:

searchedFriendsCollectionView.register(SearchFriendsCell.self, forCellWithReuseIdentifier: "SearchFriendsCell")

没有理由在每次系统请求单元格时都调用它。

关于swift - 如何在 RxDataSource 中反射(reflect)新的部分到 UICollectionView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54424130/

29 4 0
Walker 123
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress