UITableView重新排序单元格 - 重新排序/编辑模式不显示(Swift)

时间:2016-07-21 03:08:45

标签: ios swift uitableview

UITableView内有UIViewController。我试图在用户点击BarButtonItem中的NavigationBar时,表视图进入编辑模式,因此用户可以拖动并重新排序单元格。

然而,发生的事情是我只需按下编辑按钮,没有任何反应。

这就是我的尝试:

对于数组声明:

 var tester = ["1", "2", "3"]

对于按钮声明:

   @IBAction func editButtonPressed(sender: AnyObject) {
          self.editing = !self.editing
    }

对于各种tableview函数:

   func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return tester.count

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("editCell", forIndexPath: indexPath) as! EditTableViewCell
        cell.nameHolder?.text = tester[indexPath.row]



        // Configure the cell...

        return cell
    }


    func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {
        var itemToMove = tester[fromIndexPath.row]
        tester.removeAtIndex(fromIndexPath.row)
        tester.insert(itemToMove, atIndex: toIndexPath.row)

    }




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

可能导致此错误的原因是什么?

2 个答案:

答案 0 :(得分:0)

Swift 4.1

在→func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

添加此行以配置单元格:

cell.isEditing = self.tableView(tableView, canMoveRowAt: indexPath)

答案 1 :(得分:0)

Ran Hassid在其评论中对此有最正确的答案。 这样可以解决问题:

@IBOutlet weak var tableView: UITableView!
@IBAction func editButtonPressed(sender: AnyObject) {
    tableView.setEditing(!tableView.isEditing, animated: true)
}
相关问题