我的ThirdViewController在一个Xib文件中,它有一个tableView
。我在另一个xib文件中也有2个单元格,我想用RxSwift交替显示2个自定义单元格。这该怎么做。这是我的代码:
import UIKit
import RxSwift
import RxCocoa
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(UINib(nibName: "LeftTableViewCell", bundle: nil), forCellReuseIdentifier: LeftTableViewCell.reuseIdentifier)
tableView.register(UINib(nibName: "RightTableViewCell", bundle: nil), forCellReuseIdentifier: RightTableViewCell.reuseIdentifier)
items.asObservable().bindTo(tableView.rx.items(cellIdentifier: LeftTableViewCell.reuseIdentifier, cellType: LeftTableViewCell.self)) { row, data, cell in
cell.data = data
}.addDisposableTo(disposeBag)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:3)
items.asObservable()
.bindTo(tableView.rx.items) { (tableView, row, element) in
let indexPath = IndexPath(row: row, section: 0)
if row % 2 == 0 {
// or some other logic to determine which cell type to create
let cell = tableView.dequeueReusableCell(withIdentifier: LeftTableViewCell.reuseIdentifier, for: indexPath) as! LeftTableViewCell
// Configure the cell
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: RightTableViewCell.reuseIdentifier, for: indexPath) as! RightTableViewCell
// Configure the cell
return cell
}
}
.disposed(by: disposeBag)