TTBaseUICollectionView
CoreA UICollectionView subclass with sensible defaults: transparent background, hidden scroll indicators, automatic content inset for navigation bar, and reloadAsyncData() for safe background-thread data reloads.
🚀 Usage
// Default: clear bg, hidden indicators, content inset set
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumInteritemSpacing = 8
layout.minimumLineSpacing = 8
let collectionView = TTBaseUICollectionView(collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.delegate = self
// Custom bg and scroll settings
let cv = TTBaseUICollectionView(
collectionViewLayout: layout,
bgColor: .systemGroupedBackground,
isShowCroll: false,
isSetContent: true
)
// Async-safe reload (call from any thread)
viewModel.onDataUpdated = { [weak self] in
self?.collectionView.reloadAsyncData()
}
📖 API Reference
| Member | Type | Description |
|---|---|---|
bgColor | UIColor | Background color (default: .clear) |
isShowScrollIndicator | Bool | Show/hide both scroll bars (default: false) |
isSetContentInset | Bool | Auto-set top inset to account for nav bar (default: true) |
reloadAsyncData() | Method | Dispatches reloadData() on main thread safely |
setBgColor(color:) | Method | Set background color after init |
Pro Tip: Always use
reloadAsyncData() instead of calling reloadData() directly when updating from a network callback or background queue — it guarantees the reload happens on the main thread without crashing.