// // GroupViewController.swift // QuickLocation // import UIKit import RxSwift import RxCocoa import RxDataSources final class GroupViewController: BaseViewController { override var isNavigationBarHidden: Bool { true } fileprivate var rootView: GroupView! private let viewModel = GroupViewModel() override func loadView() { rootView = GroupView(frame: UIScreen.main.bounds) view = rootView } override func viewDidLoad() { super.viewDidLoad() bindViewModel() reactiveAction() observeTableViews() viewModel.loadMockData() } // MARK: - Bindings private func bindViewModel() { // 热门酷圈 viewModel.output.hotGroups .drive(rootView.hotGroupsCollectionView.rx.items( cellIdentifier: HotGroupCell.reuseId, cellType: HotGroupCell.self )) { _, item, cell in cell.configure(item) } .disposed(by: disposeBag) // 我创建的圈子 let createdDataSource = RxTableViewSectionedReloadDataSource( configureCell: { (_, tableView, indexPath, model) in let cell: CircleGroupCell = tableView.dequeueReusableCell(for: indexPath) cell.configure(model) return cell }) viewModel.output.createdSections .bind(to: rootView.createdTableView.rx.items(dataSource: createdDataSource)) .disposed(by: disposeBag) // 我加入的圈子 let joinedDataSource = RxTableViewSectionedReloadDataSource( configureCell: { (_, tableView, indexPath, model) in let cell: CircleGroupCell = tableView.dequeueReusableCell(for: indexPath) cell.configure(model) return cell }) viewModel.output.joinedSections .bind(to: rootView.joinedTableView.rx.items(dataSource: joinedDataSource)) .disposed(by: disposeBag) } private func reactiveAction() { rootView.createGroupBtn.rx.tapGesture .subscribe(onNext: { _ in // TODO: 创建圈子 }) .disposed(by: disposeBag) rootView.joinGroupBtn.rx.tapGesture .subscribe(onNext: { _ in // TODO: 加入圈子 }) .disposed(by: disposeBag) rootView.createdTabLabel.rx.tapGesture .subscribe(onNext: { [weak self] _ in self?.switchToSegment(0) }) .disposed(by: disposeBag) rootView.joinedTabLabel.rx.tapGesture .subscribe(onNext: { [weak self] _ in self?.switchToSegment(1) }) .disposed(by: disposeBag) } private func switchToSegment(_ index: Int) { rootView.selectSegment(at: index) let offset = CGPoint(x: CGFloat(index) * rootView.segmentScrollView.bounds.width, y: 0) rootView.segmentScrollView.setContentOffset(offset, animated: false) } // MARK: - 内层 tableView 滚动 → 转发给 GroupView 处理吸顶脱离 private func observeTableViews() { rootView.createdTableView.rx.didScroll .subscribe(onNext: { [weak self] in guard let self = self else { return } self.rootView.handleTableViewScroll(self.rootView.createdTableView) }) .disposed(by: disposeBag) rootView.joinedTableView.rx.didScroll .subscribe(onNext: { [weak self] in guard let self = self else { return } self.rootView.handleTableViewScroll(self.rootView.joinedTableView) }) .disposed(by: disposeBag) } }