У меня есть UIViewController (а не UITableViewController) и я создаю UITableView в loadView(). Я установил делегата и источник данных. Я реализую методы делегата canMoveRowAt и moveRowAt. Я добавляю UIBarButtonItem, по которому нужно нажать, чтобы перевести таблицу в режим редактирования, но ни один из элементов управления изменением порядка не отображается.
Код: Выделить всё
class MyTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var tableview: UITableView!
override func loadView() {
// intentionally not calling super
self.tableview = UITableView(frame: .zero, style: .grouped)
self.tableview.delegate = self
self.tableview.dataSource = self
self.view = self.tableview
}
override func viewDidLoad() {
super.viewDidLoad()
self.tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let reorderbutton = UIBarButtonItem(image: UIImage(systemName: "mount"), style: .plain, target: self, action: #selector(tappedreorder(_:)))
self.navigationItem.rightBarButtonItems = [reorderbutton]
}
@objc func tappedreorder(_ sender:Any) {
self.tableview.setEditing(true, animated: true)
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
var config = cell.defaultContentConfiguration()
config.text = ...
cell.contentConfiguration = config
return cell
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print("moved row from \(sourceIndexPath) to \(destinationIndexPath)")
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
print("can move row at \(indexPath), true")
return true
}
}
Я также пробовал добавить cell.showsReorderControl = self.tableview.isEditing в реализацию cellForRowAt, чтобы он явно запрашивал управление перемещением, но это ничего не изменило, потому что ничего не изменилось строк фактически перезагружались. Даже когда я перевел таблицу в режим редактирования, а затем выполнил reloadData(), чтобы перезагрузить все строки и установить cell.showsReorderControl, они все равно не появились.
Подробнее здесь: https://stackoverflow.com/questions/784 ... controller