apple/UIKit & ReactiveX
iOS RxSwift 07 | RxDataSources (Simple) - 커스텀 셀
lgvv
2021. 8. 21. 02:37
(init) 2021. 8. 21. 02:37
(update) 2022. 10. 05 13:10: https://rldd.tistory.com/501
✅ 와 이거 진짜로 어렵다 ....
이번 시간에는 이전 포스팅에서는 UITableViewCell에 있는 기본 셀을 사용했다고 하면, 이번에는 내가 만든 셀을 만들어서 넣어보자.
❗️ 이전 강의에서는 높이 조정도 하였으나 에러가 잡히지를 않아서 다음 포스팅에서 다루도록 하겠다.
✅ 코드 리뷰
//
// ViewController07.swift
// SnapKit_practice
//
// Created by Hamlit Jason on 2021/08/21.
//
import Foundation
import UIKit
import RxSwift
import RxCocoa
import RxDataSources
import Then
import Differentiator
class ViewController07 : UIViewController {
let tableView = UITableView().then {
$0.translatesAutoresizingMaskIntoConstraints = false
$0.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
}
let disposeBag = DisposeBag()
// var dataSource : RxTableViewSectionedAnimatedDataSource<SectionOfCustomData>?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)
tableView.snp.makeConstraints {
$0.top.leading.equalTo(10)
$0.bottom.trailing.equalTo(-10)
}
var dataSource = RxTableViewSectionedReloadDataSource<SectionOfCustomData>(
configureCell: { dataSource, tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "Item \(item.anInt): \(item.aString) - \(item.aCGPoint.x):\(item.aCGPoint.y)"
return cell
})
// self.dataSource = dataSource
dataSource.titleForHeaderInSection = { dataSource, index in
return dataSource.sectionModels[index].header
}
// dataSource.titleForFooterInSection = { dataSource, index in
// return dataSource.sectionModels[index].footer
// }
dataSource.canEditRowAtIndexPath = { dataSource, indexPath in
return true
}
dataSource.canMoveRowAtIndexPath = { dataSource, indexPath in
return true
}
let sections = [
SectionOfCustomData(header: "First section", items: [CustomData(anInt: 0, aString: "zero", aCGPoint: CGPoint.zero), CustomData(anInt: 1, aString: "one", aCGPoint: CGPoint(x: 1, y: 1)) ]),
SectionOfCustomData(header: "Second section", items: [CustomData(anInt: 2, aString: "two", aCGPoint: CGPoint(x: 2, y: 2)), CustomData(anInt: 3, aString: "three", aCGPoint: CGPoint(x: 3, y: 3)) ])
]
Observable.just(sections)
.bind(to: tableView.rx.items(dataSource: dataSource))
.disposed(by: disposeBag)
}
}
//extension ViewController07 : UITableViewDelegate {
// func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// guard let item = dataSource?[indexPath], dataSource?[indexPath.section] != nil // 1,2,3,4 섹션과 인덱스 패스 순서대로 드러가
// else {
// return 0.0
// }
//
// print("\(item) : height")
// return CGFloat(40 + item * 10)
// }
//}
// Section Model
struct SectionOfCustomData {
var header: String
var items: [Item]
}
extension SectionOfCustomData: SectionModelType {
typealias Item = CustomData
var identity: String {
return header // 헤더에 identity를 넣고
}
init(original: SectionOfCustomData, items: [Item]) {
self = original
self.items = items
}
}
// TableCellModel
struct CustomData {
var anInt: Int
var aString: String
var aCGPoint: CGPoint
}
//func == (lhs: CustomData, rhs: CustomData) -> Bool {
// return lhs.anInt == rhs.anInt && lhs.aCGPoint == rhs.aCGPoint && lhs.aString == rhs.aString
//}
✅ 다음 포스팅은
2021.08.22 - [iOS/SnapKit] - iOS Snapkit 08 | CollectionView 코드로 구성하는 법 01