Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- rxdatasources
- custom navigation bar
- swift concurrency
- swift 백준
- reactorkit
- swift navigationcontroller
- 버튼 피드백
- scene delegate
- DP
- Tuist
- swift 점선
- swift dashed line
- SWIFT
- claen architecture
- UIKit
- traits
- RxSwift
- domain data
- button configuration
- identifiable
- BFS
- uikit toast
- custombottomsheet
- task cancellation
- coordinator
- swift custom ui
- task cancel
- custom ui
- swift bottomsheet
- 타임라인 포맷팅
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift] ReuseIdentifier 하드 코딩 줄이기 본문
TableView나 CollectionView를 사용하는 경우 재사용 셀 등록 방법
tableView.register(MyTableViewCell.self, forCellReuseIdentifier: "cell")
혹은 rxSwift를 사용하는 경우
viewModel.posts.bind(to: TableView.rx.items("Cell", cellType: MyCell.self)) { row, data, cell in
...
하지만 이는 수정에 있어서 번거로움을 야기하고 오타가 날 수 도 있다는 점에서 개선의 여지가 있음
개선 방안
1. Reusable 프로토콜 정의
protocol Reusable {
static var reuseIdentifier: String { get }
}
2. (선택사항) Reuseble 확장
extension Reusable {
static var reuseIdentifier: String {
return String(describing: self)
}
}
3. Cell클래스에서 Reuseble 채택
final class MyCell: UITableViewCell, Reusable {
... }
4. 사용하기
MyTableView.register(MYCell.self, forCellReuseIdentifier: MyCell.reuseIdentifier)
'TIL' 카테고리의 다른 글
[Swift] 예약어를 변수로 사용하기 (백틱 ` `) (0) | 2025.01.09 |
---|---|
[Swift]상수 파일(Constants) 관리하기 (0) | 2025.01.09 |
[Swift] Swift의 패턴 매칭(Pattern Matching) 기능을 효과적으로 사용하는 방법은 무엇인가요? (0) | 2025.01.09 |
[Swift] Tuist를 통한 SPM 라이브러리 의존성 관리하기 (0) | 2025.01.09 |
[Swift] existential type과 any키워드(Boxed Protocol Type) (0) | 2025.01.09 |