[docs] UIKit.md: Collections & Tables section
This commit is contained in:
@@ -606,7 +606,97 @@ UIDevice.current.modelName // "iPhone 16 Pro"
|
|||||||
|
|
||||||
## Collections & Tables
|
## Collections & Tables
|
||||||
|
|
||||||
<!-- batch 12 -->
|
<details>
|
||||||
|
<summary><b>UITableView</b> — typed dequeue, safe indexing, cell animations</summary>
|
||||||
|
|
||||||
|
### `func dequeueReusableCell<T: UITableViewCell>(withClass name: T.Type) -> T` / `…(withClass:for indexPath:) -> T`
|
||||||
|
Dequeue a cell by its class name as the identifier. **Traps** if the cell isn't registered.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let cell = tableView.dequeueReusableCell(withClass: OrderCell.self, for: indexPath)
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func dequeueReusableHeaderFooterView<T: UITableViewHeaderFooterView>(withClass name: T.Type) -> T`
|
||||||
|
Same, for section header/footer views.
|
||||||
|
|
||||||
|
### `func dequeueCell<T: UITableViewCell>(indexPath: IndexPath) -> T`
|
||||||
|
Dequeue using `T.identifier` (`"id" + class name`).
|
||||||
|
|
||||||
|
### `func reloadData(_ completion: @escaping () -> Void)`
|
||||||
|
`reloadData()` with a callback for when layout settles.
|
||||||
|
|
||||||
|
### `func isValidIndexPath(_:) -> Bool`
|
||||||
|
Bounds check against the current section/row counts.
|
||||||
|
|
||||||
|
### `func safeScrollToRow(at:at scrollPosition:animated:)`
|
||||||
|
`scrollToRow` that silently no-ops for an out-of-range index path.
|
||||||
|
|
||||||
|
### `func makeMoveUpWithFadeAnimation(rowHeight:duration:delayFactor:) -> UITableViewCellAnimation`
|
||||||
|
Build a staggered slide-up + fade-in cell animation closure.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let animator = UITableViewAnimator(
|
||||||
|
animation: tableView.makeMoveUpWithFadeAnimation(rowHeight: 64, duration: 0.35, delayFactor: 0.03)
|
||||||
|
)
|
||||||
|
|
||||||
|
func tableView(_ t: UITableView, willDisplay cell: UITableViewCell, forRowAt ip: IndexPath) {
|
||||||
|
animator.animate(cell: cell, at: ip, in: t)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### `typealias UITableViewCellAnimation = (UITableViewCell, IndexPath, UITableView) -> Void`
|
||||||
|
### `class UITableViewAnimator`
|
||||||
|
`init(animation:)` + `animate(cell:at:in:)` — runs a cell animation closure.
|
||||||
|
|
||||||
|
### `UITableViewCell.identifier` / `UITableViewCell.prepareDisclosureIndicator()`
|
||||||
|
`"id" + class name`; and re-tint the disclosure chevron to a template image so it
|
||||||
|
picks up `tintColor`.
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary><b>UICollectionView</b> — setup, counts, safe indexing, carousel layout</summary>
|
||||||
|
|
||||||
|
### `static var identifier: String`
|
||||||
|
`"id" + class name`.
|
||||||
|
|
||||||
|
### `func setupCollectionView(flowLayout: = UICollectionViewFlowLayout(), spacings: = 0, direction: = .horizontal, edgesInset: = .zero, allowMulpleSelection: Bool = false, automaticSize: CGSize? = nil)`
|
||||||
|
Configure layout spacing/direction/insets, multi-selection, and self-sizing in one call.
|
||||||
|
|
||||||
|
```swift
|
||||||
|
collectionView.setupCollectionView(spacings: 8, direction: .vertical,
|
||||||
|
edgesInset: .init(top: 12, left: 16, bottom: 12, right: 16))
|
||||||
|
```
|
||||||
|
|
||||||
|
### `func reloadData(_ completion: @escaping () -> Void)`
|
||||||
|
Reload with a completion callback.
|
||||||
|
|
||||||
|
### `func numberOfItems() -> Int`
|
||||||
|
Total items across all sections.
|
||||||
|
|
||||||
|
### `var lastSection: Int` / `var indexPathForLastItem: IndexPath?` / `func indexPathForLastItem(inSection:) -> IndexPath?`
|
||||||
|
Last section index; index path of the last item overall or in a section.
|
||||||
|
|
||||||
|
### `func isValidIndexPath(_:) -> Bool` / `func safeScrollToItem(at:at scrollPosition:animated:)`
|
||||||
|
Bounds check; scroll that no-ops on an invalid index path.
|
||||||
|
|
||||||
|
### `enum CollectionViewFlowLayoutSpacingMode`
|
||||||
|
`.fixed(spacing:)` / `.overlap(visibleOffset:)` — spacing strategy for the carousel layout below.
|
||||||
|
|
||||||
|
### `open class CollectionViewFlowLayout: UICollectionViewFlowLayout`
|
||||||
|
A centred, paginated "cover-flow" style layout: the centred item is full size,
|
||||||
|
side items scale and fade. Tunables: `sideItemScale` (0.6), `sideItemAlpha`
|
||||||
|
(0.6), `sideItemShift` (0), `spacingMode` (`.fixed(40)`).
|
||||||
|
|
||||||
|
```swift
|
||||||
|
let layout = CollectionViewFlowLayout()
|
||||||
|
layout.itemSize = CGSize(width: 240, height: 320)
|
||||||
|
layout.sideItemScale = 0.7
|
||||||
|
layout.spacingMode = .overlap(visibleOffset: 30)
|
||||||
|
collectionView.collectionViewLayout = layout
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
## Media & Components
|
## Media & Components
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user