sorting - 使用点击在表格 View 中移动行 - Swift

标签 sorting swift tableview cell

我尝试通过单击单元格来模拟单元格的拖动。

点击后,单个单元格必须是列表中的第一项。 我尝试了所有方法,但我的索引路径发生了变化,但我没有在表中看到结果,即使我重新加载它也是如此。

有什么建议吗?

这是我的代码:

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)
}

}

谢谢

最佳答案

要以动画方式交换单元格的位置,您需要同时更新数据源和 tableView 的表示。你可以这样做:

table.beginUpdates()

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

table.endUpdates()

关于sorting - 使用点击在表格 View 中移动行 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26855705/

相关文章:

c# - 如何按数字样式对字符串数组进行排序?

java - Map.Entry.<Integer, Integer>comparingByValue() 中 <Integer,Integer> 的重要性是什么

ios - SceneKit 物理模拟与实际节点位置不匹配

swift - 明暗不切换,分段控制(Swift)

swift - 在 Swift 中将数据从 tableView 传递到 ViewController

sorting - 列出 ASP Classic 中的最新文件

python - 什么是查找重复对象的好 pythonic 方法?

iOS:UIView 外的实心边框

iphone - 当我点击表格 View 中的第一个单元格时,不会触发 didSelectRowAtIndexPath

ios - 从 didSelectRowAtIndexPath 发送到实例的无法识别的选择器