如何使用动画更改单元格编辑样式

时间:2016-10-19 04:42:30

标签: swift uitableview swift3 ios10

在iOS 10的Clock应用程序中,在编辑和默认样式之间切换单元格会带来平滑的动画过渡(如下面的录制中所示)

clock app cell style change animated

我正在尝试为包含tableview的简单应用复制此转换,但我不知道如何实现动画转换。

My app with no animation change

我的编辑样式更改的应用代码位于

之下

ViewController.swift

...

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    //Model array for cell data - omitted population of array for simplicity
    var notes = [Note]()

    //cell editing style
    var cellStyleForEditing: UITableViewCellEditingStyle = .none

    //The tableview
    @IBOutlet weak var NoteTable: UITableView!
    ...

    @IBAction func enableEdittingBttn(_ sender: AnyObject) {

        if(cellStyleForEditing == .none) {
            cellStyleForEditing = .delete
            self.NoteTable.reloadData()
        } else {
            cellStyleForEditing = .none
            self.NoteTable.reloadData()
        }
    }

    //delegate function sets cell editing style on table load/reload
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
        return cellStyleForEditing
    }

    ...

    //omitted tableview delegate methods 
}

正如您所看到的,我通过在更改表格的单元格编辑样式后重新加载表格数据来实现单元格样式的更改。

为简单起见,我省略了所有不相关的代码。

1 个答案:

答案 0 :(得分:2)

在启用或禁用编辑时,不要重新加载表格数据,而是使用动画true调用setEditing(_:animated:)

if(cellStyleForEditing == .none) {
    cellStyleForEditing = .delete
} else {
    cellStyleForEditing = .none
}
NoteTable.setEditing(cellStyleForEditing != .none, animated: true)
相关问题