RxSwift Tableview

in #wwwww7 years ago (edited)

셀 타입이 여러가지일 경우 데이터 바인딩

// 셀 타입이 여러가지일 경우에는 ViewModel에서 가공된 CellType(Enum)으로 분기를 하여 처리한다.
        self.viewModel.items.asObservable().bind(to: self.tableView.rx.items) { (tableview, row, item) in
            switch item {
            case .notice(let value):
                let cell = self.tableView.dequeueReusableCell(withIdentifier: "NoticeCell") as! NoticeCell
                cell.date?.text = self.dateFormat(string: value.creationDate)
                cell.title?.text = value.title
                return cell
            case .exemple(let value):
                let cell = self.tableView.dequeueReusableCell(withIdentifier: "ExCell") as! ExCell
                cell.title?.text = value
                return cell
            }
            return UITableViewCell()
            }.disposed(by: disposeBag)

셀 타입이 유일한 경우의 데이터바인딩

// 셀 타입이 유일한경우 bind(to self.tableview.tx.items(cellIdentifier: "CellName", cellType: CellClass.seld)) { (row, element, cell) in }
        self.viewModel.items.asObservable().bind(to: (self.tableView?.rx.items(cellIdentifier: "NoticeCell", cellType: NoticeCell.self))!) { (row, element, cell) in
                cell.title?.text = element.title
                cell.date?.text = self.dateFormat(string: element.creationDate)
        }.disposed(by: disposeBag)

셀타입이 유일한 셀을 선택했을 경우의 로직 바인딩

// 테이블뷰의 셀을 선택했을시의 로직을 바인딩해준다.{ (indexPath) in}
        self.tableView.rx.itemSelected.bind { (indexPath) in
            let destination = self.storyboard?.instantiateViewController(withIdentifier: "CommonWebViewController") as! CommonWebViewController
            destination.urlString = self.viewModel.items.value[indexPath.row].url
            self.present(destination, animated: true, completion: nil)
        }.disposed(by: disposeBag)

셀타입이 여러가지일 경우 셀을 선택했을 경우의 로직 바인딩

------ 셀타입이 여러가지일 경우는 cellType의 배열을 생성하여 switch case로 분기하여 처리한다.------

// 테이블뷰의 셀을 선택했을시의 로직을 바인딩해준다.
        self.tableView.rx.itemSelected.bind { (indexPath) in
            let cellType = self.viewModel.items.value[indexPath.row]
            switch cellType {
            case .notice(let value):
                let destination = self.storyboard?.instantiateViewController(withIdentifier: "CommonWebViewController") as! CommonWebViewController
                destination.urlString = value.url
                self.present(destination, animated: true, completion: nil)
            case .exemple(let value):
                QL1(value)
            }
        }.disposed(by: disposeBag)

******마지막에는 꼭 disposed를 체이닝하여 메모리에서 해제하게 해 주어야 한다. *******

TableView(RxSwift) 데이터 바인딩

보통 Row와 Section은 각각 cellType, sectionType이라고 enum타입으로 정의를 한 후 Array를 생성하여 뷰에맞게 append해준다.

Switch / Case 로 분기를 태워서 tableView의 구성을 잡는다.

* 데이터의 가광과 정렬은 해당 view의 viewmodel에서 처리하여 view단에서는 observer만 할 수 있도록 구현한다(단지 view에서는 가공된 데이터를 테이블뷰에 바인딩시키는 역활만 할 뿐이다.)

Rx(viewModel)의 BehaviorRelay는 변수 즉 variable이라고 봐도 무방하다.

Sort:  

Congratulations @jinsky! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @jinsky! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.15
TRX 0.23
JST 0.032
BTC 88405.67
ETH 2382.08
USDT 1.00
SBD 0.68