반응형
개발하는 정대리 개인 미션 과제
1. URLSession과 RxSwift를 이용해 API를 호출
2. RxSwift에서 제공하는 TableView를 사용
3. Error 처리
4. Paging 처리
TableView Delegate와 DataSource 사용
//MARK: - TableView DataSource
extension FirstVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.mocksVM.mocks.value.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "MockCell", for: indexPath) as? MockCell else {
return UITableViewCell()
}
let cellData = self.mocksVM.mocks.value[indexPath.row]
guard let id = cellData.id,
let email = cellData.email,
let avatar = cellData.avatar,
let title = cellData.title,
let content = cellData.content else { return UITableViewCell() }
cell.idLabel.text = "\(id)"
cell.emailLabel.text = email
cell.avatarLabel.text = avatar
cell.titleLabel.text = title
cell.contentLabel.text = content
cell.layer.borderWidth = 1
cell.layer.borderColor = UIColor.textPoint?.cgColor
cell.layer.cornerRadius = 4
return cell
}
}
//MARK: - TableView Delegate
extension FirstVC: UITableViewDelegate {
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = FirstDetailVC()
guard let id = self.mocksVM.mocks.value[indexPath.row].id else { return }
vc.title = "\(id)"
self.navigationController?.pushViewController(vc, animated: true)
}
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "삭제") { (action, view, completionHandler) in
self.showDeleteAlert() {
guard let id = self.mocksVM.mocks.value[indexPath.row].id else {return}
self.mocksVM.deleteMocksItem(id: id)
completionHandler(true)
}
completionHandler(false)
}
deleteAction.backgroundColor = .red
return UISwipeActionsConfiguration(actions: [deleteAction])
}
}
RxSwift에서 제공하는 TableView 사용
self.mocksVM
.mocks
.bind(to:self.mockTableView.rx.items(cellIdentifier: "MockCell", cellType: MockCell.self)) {[weak self] idx, cellData, cell in
guard let self = self else { return }
cell.updateUI(cellData)
}
.disposed(by: disposeBag)
self.mockTableView
.rx
.itemSelected
.subscribe(onNext: {data in
let vc = SecondDetailVC()
guard let id = self.mocksVM.mocks.value[data.row].id else { return }
vc.title = "\(id)"
self.navigationController?.pushViewController(vc, animated: true)
})
.disposed(by: disposeBag)
self.mockTableView
.rx
.estimatedRowHeight
.onNext(UITableView.automaticDimension)
self.mockTableView
.rx
.itemDeleted
.subscribe({ data in
guard let indexPath = data.element,
let id = self.mocksVM.mocks.value[indexPath.row].id else {return}
self.mocksVM.deleteMocksItem(id: id)
})
.disposed(by: disposeBag)
반응형
'Mobile > iOS' 카테고리의 다른 글
[iOS] StatusBar & SearchBar 설정 (0) | 2023.05.31 |
---|---|
[iOS] SearchBar PlaceHolder 색상 변경 방법 (0) | 2023.05.31 |
[Project] Mission 01 (0) | 2023.05.27 |
iOS 개발 꿀팁 사이트 (0) | 2023.05.17 |
[iOS] 커스텀 폰트 적용하는 방법 (코드UI) (0) | 2023.04.06 |