在TableView中移动单元格

时间:2016-04-26 04:46:33

标签: swift tableview

我想在.dropdown-tracks { position: relative; z-index: 100; } .container { position: relative; z-index: 50; } 中移动单元格(重新排序单元格)。

我试过这样的话:

TableView

我还在import UIKit class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet var editBarButton: UIBarButtonItem! var tableData = ["One","Two","Three","Four","Five"] override func viewDidLoad() { super.viewDidLoad() } func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return tableData.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) cell.textLabel!.text = tableData[indexPath.row] return cell } func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool { return true } func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { let itemToMove = tableData[sourceIndexPath.row] tableData.removeAtIndex(sourceIndexPath.row) tableData.insert(itemToMove, atIndex: destinationIndexPath.row) } @IBAction func editBarButtonTapped(sender: AnyObject) { self.editing = !self.editing } } 中设置了TableViewDataSourceTableViewDelegate

但是当我点击Storyboard时,它不会造成任何影响。

我想这样做:

enter image description here

任何帮助将不胜感激。感谢。

2 个答案:

答案 0 :(得分:3)

import UIKit

class FirstViewController: UITableViewController {

    var dataHolder: Array = ["One","Two","Three","Four","Five"]

    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

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

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return dataHolder.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath)

        cell.textLabel?.text = dataHolder[indexPath.row]

        return cell
    }

    override func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle {
        return UITableViewCellEditingStyle.None
    }

    override func tableView(tableView: UITableView, shouldIndentWhileEditingRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }

    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

        let itemToMove: String = dataHolder[fromIndexPath.row]
        dataHolder.removeAtIndex(fromIndexPath.row)
        dataHolder.insert(itemToMove, atIndex: toIndexPath.row)

    }

    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
}

如果您未使用UITableViewController,请确保您已实施以下方法:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    tableView.setEditing(editing, animated: animated)
}

答案 1 :(得分:1)

现在我找到了答案。如果我不使用UITableViewController,我需要实现此功能:

override func setEditing(editing: Bool, animated: Bool) {
    super.setEditing(editing, animated: animated)
    tableView.setEditing(editing, animated: animated)
}

因为这个函数说:

  

设置视图控制器是否显示可编辑视图。子类   使用edit-done按钮必须覆盖此方法才能更改它们   如果编辑为真且不可编辑状态,则查看可编辑状态   如果是假的这个方法应该调用super的实现   在更新其视图之前。

现在有效。