class MainViewController: UIViewController {
public var seletedIndex: Int = 0
lazy var tableView: UITableView = {
let table = UITableView()
table.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
return table
}()
private var btn: UIButton = {
let btn = UIButton()
btn.setTitle("", for: .normal)
btn.setTitleColor(UIColor.black, for: .normal)
return btn
}()
public var items = [PayMenmet(name: "", icon: "alipay"),PayMenmet(name: "", icon: "wechatpay")]
lazy var mainView: MainView = MainView()
private let disposeBag = DisposeBag()
lazy var service: MainService = MainService.instance
override func viewDidLoad() {
super.viewDidLoad()
initView()
initBindData()
}
func initView() {
self.view.addSubview(tableView)
self.view.addSubview(btn)
tableView.snp.makeConstraints { make in
make.center.equalToSuperview()
make.left.right.equalToSuperview()
make.height.equalTo(120)
}
btn.snp.makeConstraints { make in
make.top.equalTo(tableView.snp.bottom).offset(10)
make.left.right.equalToSuperview().offset(20)
}
}
func initBindData() {
tableView.delegate = self
tableView.dataSource = self
tableView.selectRow(at: IndexPath(row: seletedIndex, section: 0), animated: true, scrollPosition: .none)
btn.rx.tap.subscribe(onNext: { [weak self] in
})
}
}
extension MainViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
cell.textLabel?.text = items[indexPath.row].name
cell.imageView?.image = UIImage(named: items[indexPath.row].icon)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("indexPath::",indexPath)
let cell = self.tableView.cellForRow(at: indexPath)
cell?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath)
cell?.accessoryType = .none
}
}
the result is that although the first row is selected, the didSelectRowAt is not executed, so the accessoryType does not take effect. What is the reason for this?