2016-04-26 74 views
1

我想在TableView中移动单元格(重新排序单元格)。在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 
    } 
} 

我也曾在Storyboard设置TableViewDataSourceTableViewDelegate

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

我想让它像这样:

enter image description here

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

回答

1

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

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

因为这个函数说:视图控制器

设置是否显示了一个可编辑视图。使用编辑完成按钮的子类 必须覆盖此方法,以便在编辑为真时将它们的 视图更改为可编辑状态,如果编辑为真,则将其视为不可编辑状态 。在更新视图之前,此方法应该调用超级实现 。

现在有效。

2
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) 
} 
+0

谢谢。但它不起作用。 – Khuong

+0

@ khuong291什么不能正确工作?你需要比“但它不起作用”更具体。 – fuzz

+0

当我点击编辑栏按钮。它不会造成任何影响。你的代码是为UITableViewController实现的。我已经像你一样做了,但没有工作。 – Khuong