【问题标题】:Moving Row inside a Table View with tap - Swift用点击在表格视图中移动行 - Swift
【发布时间】:2015-01-07 11:01:43
【问题描述】:

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

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

有什么建议吗?

这是我的代码:

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

}

谢谢

【问题讨论】:

  • 我不太明白你想要完成什么。你想让你点击的任何单元格移到列表顶部吗?
  • 是的!正是这一点,但动画也是如此。谢谢你的回复

标签: sorting swift tableview cell


【解决方案1】:

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

table.beginUpdates()

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

table.endUpdates()

【讨论】:

    猜你喜欢
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多