修复首页账号切换布局崩溃

This commit is contained in:
2026-07-07 14:34:31 +08:00
parent 3ee408daf1
commit bbce26af75
3 changed files with 31 additions and 8 deletions
+2 -2
View File
@@ -35,8 +35,8 @@ enum HomeCollectionLayoutBuilder {
/// 构建 Compositional Layout,按当前分区类型返回 section 布局。
static func makeLayout(sections: [HomeCollectionSection]) -> UICollectionViewCompositionalLayout {
UICollectionViewCompositionalLayout { sectionIndex, environment in
guard sectionIndex < sections.count else { return nil }
switch sections[sectionIndex] {
let section = sectionIndex < sections.count ? sections[sectionIndex] : .commonApps
switch section {
case .workStatus:
return fullWidthSection(height: AppSpacing.formRowHeight)
case .locationReport:
+26 -6
View File
@@ -9,6 +9,8 @@ import UIKit
/// 首页 Tab 根页面,按身份与权限展示信息与入口。
final class HomeViewController: BaseViewController {
private static let fallbackCellReuseIdentifier = "HomeFallbackCell"
private let viewModel = HomeViewModel()
private let homeAPI = NetworkServices.shared.homeAPI
@@ -117,6 +119,10 @@ final class HomeViewController: BaseViewController {
private func registerCollectionCells() {
collectionView.register(HomeWorkStatusCell.self, forCellWithReuseIdentifier: HomeWorkStatusCell.reuseIdentifier)
collectionView.register(
UICollectionViewCell.self,
forCellWithReuseIdentifier: Self.fallbackCellReuseIdentifier
)
collectionView.register(
HomeLocationReportCell.self,
forCellWithReuseIdentifier: HomeLocationReportCell.reuseIdentifier
@@ -138,7 +144,18 @@ final class HomeViewController: BaseViewController {
let dataSource = UICollectionViewDiffableDataSource<HomeCollectionSection, HomeCollectionItem>(
collectionView: collectionView
) { [weak self] collectionView, indexPath, item in
guard let self else { return UICollectionViewCell() }
guard let self else {
return collectionView.dequeueReusableCell(
withReuseIdentifier: Self.fallbackCellReuseIdentifier,
for: indexPath
)
}
guard indexPath.section < self.currentSections.count else {
return collectionView.dequeueReusableCell(
withReuseIdentifier: Self.fallbackCellReuseIdentifier,
for: indexPath
)
}
let section = self.currentSections[indexPath.section]
switch (section, item) {
case (.workStatus, .workStatus):
@@ -217,14 +234,15 @@ final class HomeViewController: BaseViewController {
return cell
default:
return UICollectionViewCell()
return collectionView.dequeueReusableCell(
withReuseIdentifier: Self.fallbackCellReuseIdentifier,
for: indexPath
)
}
}
dataSource.supplementaryViewProvider = { collectionView, kind, indexPath in
guard kind == UICollectionView.elementKindSectionHeader,
indexPath.section < self.currentSections.count,
self.currentSections[indexPath.section] == .commonApps else {
guard kind == UICollectionView.elementKindSectionHeader else {
return nil
}
let header = collectionView.dequeueReusableSupplementaryView(
@@ -232,7 +250,9 @@ final class HomeViewController: BaseViewController {
withReuseIdentifier: HomeSectionHeaderView.reuseIdentifier,
for: indexPath
) as! HomeSectionHeaderView
header.apply(title: "常用应用")
let title = indexPath.section < self.currentSections.count
&& self.currentSections[indexPath.section] == .commonApps ? "常用应用" : ""
header.apply(title: title)
return header
}