【问题标题】:How to change cell editing style with animation如何使用动画更改单元格编辑样式
【发布时间】:2017-02-28 13:21:17
【问题描述】:

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

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

我的编辑样式更改的应用代码如下

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 
}

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

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

【问题讨论】:

    标签: swift uitableview swift3 ios10


    【解决方案1】:

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

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

    【讨论】:

    • 您能否提供更多信息:)
    • 如果这已经解决了您的问题,请接受答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 1970-01-01
    相关资源
    最近更新 更多