我有UITableViewController
的以下子类:
final public class ILoveAppleSelectorTableViewController: UITableViewController {
public weak var delegate: ILoveAppleSelectorTableViewControllerDelegate?
public var data = [ILoveAppleGroupModel]()
convenience init() {
self.init(style: .plain)
}
convenience init(data: [ILoveAppleGroupModel]) {
self.init(style: .plain)
self.data = data
}
override public func viewDidLoad() {
super.viewDidLoad()
title = "Select APple Product"
tableView.registerCellClass(CheckmarkTableViewCell.self)
tableView.allowsSelection = true
tableView.allowsMultipleSelection = false
}
// MARK: - Selection
public func selectItem(at indexPath: IndexPath) {
tableView.selectRow(at: indexPath, animated: false, scrollPosition: .none)
}
// MARK: - Table view data source
override public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CheckmarkTableViewCell = tableView.dequeueCell(for: indexPath)
let item = data[indexPath.row]
cell.textLabel?.text = item.material_group_name
return cell
}
override public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedappleProduct = data[indexPath.row]
delegate?.ILoveAppleSelector(controller: self, didSelect: selectedappleProduct)
}
}
我创建它的一个实例并调用selectItem
方法:
let data = Array(appleProducts)
let selector = AppleProductSelector(data: data)
selector.selectItem(at: IndexPath(indexes: [0, 0]))
navigationController?.pushViewController(selector, animated: true)
我在调试器中检查到selectItem
方法已成功调用,但是未选择任何内容。
如果我在viewWillAppear
上选择了一个项目,则一切正常,并且该项目显示为选中状态:
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
selectItem(at: selectedIndexPath)
}