class TableViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var array = ["1","2","3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuseIdentifier")
tableView.delegate = self
tableView.isScrollEnabled = false
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return array.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
cell.textLabel?.text = array[indexPath.section]
return cell
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let item = array[sourceIndexPath.section]
array[sourceIndexPath.section] = array[destinationIndexPath.section]
array[destinationIndexPath.section] = item
tableView.beginUpdates()
tableView.reloadData()
tableView.endUpdates()
}
}
extension TableViewController: UITableViewDragDelegate {
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
return [UIDragItem(itemProvider: NSItemProvider())]
}
}
extension TableViewController: UITableViewDropDelegate {
func tableView(_ tableView: UITableView, performDropWith coordinator: UITableViewDropCoordinator) {
}
func tableView(_ tableView: UITableView, dropSessionDidUpdate session: UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) -> UITableViewDropProposal {
if tableView.hasActiveDrag {
if session.items.count > 1 {
return UITableViewDropProposal(operation: .cancel)
} else {
return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
}
} else {
return UITableViewDropProposal(operation: .copy, intent: .insertAtDestinationIndexPath)
}
}
}
Этот код вылетает с этой ошибкой, когда я пытаюсь изменить порядок ячеек. Это прекрасно работает, когда вместо раздела я делаю это в строках.
Вот журнал ошибок:
TableViewSample [45038: 397348] *** Завершение работы приложения из-за необработанного исключения «NSInternalInconsistencyException», причина: «попытка переместить путь индекса ({ длина = 2, путь = 2 - 0}) к пути индекса ({длина = 2, путь = 0 - 1}), который не существует - в разделе 0 после обновления есть только 1 строка '