Ios 在两个分区之间移动UITableViewCell

Ios 在两个分区之间移动UITableViewCell,ios,swift,Ios,Swift,我用了两个部分,比如, 第1节 单元格0 第1单元 第3单元和 第2节 单元格0 第1单元 第三单元 但是我想在第1节中移动第2节的单元格0 有人能用swift编程的代码向我解释一下吗 tableView.moveRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 2), toIndexPath: NSIndexPath(forRow: 0, inSection: 1)) 在调用此方法之前,请不要忘记更新您的数据模型。基本上,您需要了解在交换单元格

我用了两个部分,比如, 第1节 单元格0 第1单元 第3单元和 第2节 单元格0 第1单元 第三单元

但是我想在第1节中移动第2节的单元格0 有人能用swift编程的代码向我解释一下吗

tableView.moveRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 2), toIndexPath: NSIndexPath(forRow: 0, inSection: 1))

在调用此方法之前,请不要忘记更新您的数据模型。

基本上,您需要了解在交换单元格之前,数据源需要更新,否则会发生崩溃

请看以下示例:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var data : [[String]] = [["Mike", "John", "Jane"], ["Phil", "Tania", "Monica"]]

    @IBOutlet weak var tableView: UITableView!
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return data.count
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data[section].count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as UITableViewCell
        let name = data[indexPath.section][indexPath.row]
        (cell.viewWithTag(22) as UILabel).text = "(" + String(indexPath.section) + ":" + String(indexPath.item) + ") " + name

        return cell
    }

    @IBAction func pressed(sender: UIButton) {

        // arbitrarily define two indexPaths for testing purposes
        let fromIndexPath = NSIndexPath(forRow: 0, inSection: 0)
        let toIndexPath = NSIndexPath(forRow: 0, inSection: 1)

        // swap the data between the 2 (internal) arrays
        let dataPiece = data[fromIndexPath.section][fromIndexPath.row]
        data[toIndexPath.section].insert(dataPiece, atIndex: toIndexPath.row)
        data[fromIndexPath.section].removeAtIndex(fromIndexPath.row)

        // Do the move between the table view rows
        self.tableView.moveRowAtIndexPath(fromIndexPath, toIndexPath: toIndexPath)


    }
}

这里我有一个最简单的二维数组的例子,它包含一些名称。我将其定义为[[String]],以避免以后强制转换它。在我的故事板中的按钮之前,我有一个称为“按下”的按钮。我交换数据源,然后调用MoveRowatineXpath。

您可以交换数据源中的值并准确地重新加载表格,但请您用代码向我解释一下,因为我在ios开发方面是新手。我可以问与
moveRow
相关的问题吗?