我想在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
设置TableViewDataSource
和TableViewDelegate
。
但是当我点击editBarButton
时,它不会造成任何影响。
我想让它像这样:
任何帮助,将不胜感激。谢谢。
谢谢。但它不起作用。 – Khuong
@ khuong291什么不能正确工作?你需要比“但它不起作用”更具体。 – fuzz
当我点击编辑栏按钮。它不会造成任何影响。你的代码是为UITableViewController实现的。我已经像你一样做了,但没有工作。 – Khuong