Moving Row inside a Table View with tap - Swift

删除回忆录丶 提交于 2019-12-06 08:33:27

问题


I'm try to simulate the dragging of the cell with the single tap on the cell.

Once tapped the single cell has to be the first item in the list. I tried with ll the method but my indexpath change but I don't see the result in the table, even if i reload it.

Any Suggestion?

here's my code:

import UIKit

var array = ["1", "2", "3", "4", "5", "6"]

class Table: UITableViewController {


override func viewDidLoad() {
    super.viewDidLoad()

            tableView.setEditing(true, animated: true)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return array.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
    cell.textLabel.text = array[indexPath.row]
    return cell

}

    var indexPathForCell = NSIndexPath(forRow: 0, inSection: 0)

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPathForCell: NSIndexPath) {
    tableView.reloadData()
    println(indexPathForCell)

//        

}

override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath indexPathForCell: NSIndexPath) {
    println(indexPathForCell)
}

}

Thank you


回答1:


To swap a cell's position animated, you'll need to update your data source and the tableView's representation at the same time. You can do that like this:

table.beginUpdates()

table.moveRowAtIndexPath(indexPath, toIndexPath: NSIndexPath(forRow: 0, inSection: 0))
array.insert(array.removeAtIndex(indexPath.row), atIndex: 0)

table.endUpdates()


来源:https://stackoverflow.com/questions/26855705/moving-row-inside-a-table-view-with-tap-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!