Signal-iOS/SignalUI/Categories/UITableView+ReusableCell.swift
Max Radermacher 9439cc5191
Clean up recipient picker contact/group cells
- Move recipient picker code to the correct file.

- Add a helper to fetch typed reusable table view cells.

- Only set shouldUseAsyncSelection in the one place that uses it.

- Remove some code that was duplicated between sync/async selection.

- Fix some strong references in the async preparation flow.

- Allow network errors when preparing recipients -- these may do network
  fetches, and those can fail for non-interesting reasons.

- Move FindByPhoneNumberDelegate to Swift & make some code private.
2022-11-07 13:02:45 -08:00

47 lines
1.7 KiB
Swift

//
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
//
import UIKit
public protocol ReusableTableViewCell {
static var reuseIdentifier: String { get }
}
public extension UITableView {
/// A typed wrapper around `dequeueReusableCell`.
///
/// - Parameters:
/// - type: The `UITableViewCell` subclass that should be dequeued.
/// - indexPath: An optional index path for where the cell will be shown.
/// - isRegistered:
/// If true, indicates that the class was registered using
/// `-[UITableView registerClass:forCellReuseIdentifier:]`; in this
/// case, the table view will create a new instance if one isn't
/// available to dequeue. If false, indicates that the class wasn't
/// registered; in this case, the table view will return `nil` if a
/// cell isn't available to dequeue.
/// - Returns:
/// A typed `UITableViewCell` subclass. If `isRegistered` is true (and
/// the class is properly registered), this won't return `nil`.
func dequeueReusableCell<T: ReusableTableViewCell & UITableViewCell>(
_ type: T.Type,
for indexPath: IndexPath? = nil,
isRegistered: Bool = true
) -> T? {
let untypedCell: UITableViewCell?
if let indexPath {
untypedCell = dequeueReusableCell(withIdentifier: type.reuseIdentifier, for: indexPath)
} else {
untypedCell = dequeueReusableCell(withIdentifier: type.reuseIdentifier)
}
guard let typedCell = untypedCell as? T else {
owsAssertDebug(!isRegistered, "Registered cells should exist and have the correct type.")
return nil
}
return typedCell
}
}