使用Tap在表格视图内移动行-Swift [英] Moving Row inside a Table View with tap - Swift

查看:149
本文介绍了使用Tap在表格视图内移动行-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过单击单元格来模拟该单元格的拖动.

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

轻按一次,单个单元格必须是列表中的第一项. 我尝试使用ll方法,但是索引路径发生了变化,但是即使重新加载,我也没有在表中看到结果.

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.

有什么建议吗?

这是我的代码:

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的表示形式.您可以这样做:

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

这篇关于使用Tap在表格视图内移动行-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆