Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 获取节时重新排序时TableView崩溃_Ios_Swift_Uitableview - Fatal编程技术网

Ios 获取节时重新排序时TableView崩溃

Ios 获取节时重新排序时TableView崩溃,ios,swift,uitableview,Ios,Swift,Uitableview,} 当我尝试重新排列单元格时,此代码因此错误而崩溃。它的工作非常好,而不是节我做它的行 以下是错误日志: TableViewSample[45038:397348]***由于未捕获而终止应用程序 异常“NSInternalInconsistencyException”,原因:“尝试移动” 索引路径({length=2,path=2- 0})到索引路径({length=2,path =0-1})不存在-更新后第0节中只有1行 您切换了这两种方法的返回值: func numberOfSections

}

当我尝试重新排列单元格时,此代码因此错误而崩溃。它的工作非常好,而不是节我做它的行

以下是错误日志:

TableViewSample[45038:397348]***由于未捕获而终止应用程序 异常“NSInternalInconsistencyException”,原因:“尝试移动” 索引路径({length=2,path=2- 0})到索引路径({length=2,path =0-1})不存在-更新后第0节中只有1行


您切换了这两种方法的返回值:

func numberOfSections(在tableView:UITableView中)->Int{
//#警告未完成执行,返回节数
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
//#警告未完成实现,返回行数
返回数组.count
}

Yes@koen我知道这是可行的,但由于其他一些条件,我们希望以节的形式进行,然后在每个节中进行一行。您会重新订购什么?你能详细说明一下吗?或者重新排序?显然,您使用的sourceIndexPath和destinationIndexPath参数不正确。
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)
        }
    }
}