Register two custom cells with RxSwift

I have a ViewControllerconnection to the Xib file. In this Xib file, I have a view, and inside this view I have a TableView similar to

enter image description here

I also have LeftTableViewCelland RightTableViewCellwhich are associated with other Xib files.

enter image description here

enter image description here

Now I want to register LeftTableViewCelland RightTableViewCellin a TableView it, so how to do it, my code is always can not compile.

class ThirdViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    let items: Variable<[String]> = Variable(["Test 2", "Test 3", "Test 1", "Test 4", "Test 5"])

    let disposeBag = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

//        tableView.register(LeftTableViewCell.self, forCellReuseIdentifier: LeftTableViewCell.reuseIdentifier)

//        tableView.register(UINib(nibName: "LeftTableViewCell", bundle: nil), forCellReuseIdentifier: LeftTableViewCell.reuseIdentifier)
//        tableView.register(UINib(nibName: "RightTableViewCell", bundle: nil), forCellReuseIdentifier: LeftTableViewCell.reuseIdentifier)

    items.asObservable().bindTo(tableView.rx.items(cellIdentifier: LeftTableViewCell.reuseIdentifier, cellType: LeftTableViewCell.self)) { row, data, cell in
        cell.data = data
        }.addDisposableTo(disposeBag)

    tableView.rx.modelSelected(String.self).subscribe { [unowned self] (event) in
        switch event {
        case .next(let element):
            if let selectedRowIndexPath = self.tableView.indexPathForSelectedRow {
                self.tableView.deselectRow(at: selectedRowIndexPath, animated: true)
            }

            break
        default:
            break
        }
        }.addDisposableTo(disposeBag)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }

}
+4
source share
2 answers

Register XIBs

Use the familiar register function and register each XIB with a different identifier.

tableView.register(UINib(nibName: "LeftTableViewCell", bundle: nil), forCellReuseIdentifier: "LeftCell")
tableView.register(UINib(nibName: "RightTableViewCell", bundle: nil), forCellReuseIdentifier: "RightCell")

Return the cell you want

factory:

  • , .

.

items.asObservable()
    .bindTo(tableView.rx.items) { (tableView, row, element) in
        let indexPath = IndexPath(row: row, section: 0)

        if row % 2 == 0 {    // 1


            let cell = tableView.dequeueReusableCell(withIdentifier: "LeftCell", for: indexPath) as! LeftTableViewCell    // 2
            // Configure the cell
            return cell    // 3
        }
        else {
            let cell = tableView.dequeueReusableCell(withIdentifier: "RightCell", for: indexPath) as! RightTableViewCell
            // Configure the cell
            return cell
        }

    }
    .disposed(by: disposeBag)

+4

nib

  • UITableViewCell xib
  • View

tableView.register(UINib (nibName: "CustomCell" , bundle: nil), forCellWithReuseIdentifier: "CustomCell" )

cellForRowAtIndexPath

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CustomCell" , : indexPath) ! CustomCell

0

Source: https://habr.com/ru/post/1664053/


All Articles