Ios 如何使用动画折叠tableview?

Ios 如何使用动画折叠tableview?,ios,swift,uitableview,Ios,Swift,Uitableview,我有一个包含两个部分的tableview var categories = ["a", "b", "c"] var items = ["1" , "2", "3"]//will be different based on category selection var selectedCategory: String? 最初,第一部分仅可见。用户选择第0节中的任何一行后,第1节将有3行,第0节将只有选定的行 例如,如果选择“b”类,则应删除“a”、“c”行,并添加“1”、“2”、“3”行 现在,

我有一个包含两个部分的tableview

var categories = ["a", "b", "c"]
var items = ["1" , "2", "3"]//will be different based on category selection
var selectedCategory: String?
最初,第一部分仅可见。用户选择第0节中的任何一行后,第1节将有3行,第0节将只有选定的行

例如,如果选择“b”类,则应删除“a”、“c”行,并添加“1”、“2”、“3”行

现在,如果取消选择“b”,则应添加“a”、“c”行,并删除“1”、“2”、“3”行

//UITableViewDataSource

func numberOfSections(in tableView: UITableView) -> Int {
    return selectedCategory == nil ? 1 : 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return section == 0 ? categories.count : items.count
}
我已经使用
tableView.reloadData()
完成了这项工作。它不显示任何动画。我试图通过以下方法实现这一点

tableView.deleteRows(at: [IndexPath], with: UITableViewRowAnimation)
tableView.insertRows(at: [IndexPath], with: UITableViewRowAnimation)

我正在努力让IndExpath插入和删除

而不是重新加载整个tableView。请执行以下操作:-

tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath], with: UITableViewRowAnimation.top)
tableView.insertRows(at: [IndexPath], with: UITableViewRowAnimation.top)
tableView.endUpdates()

不要重新加载整个tableView,请执行以下操作:-

tableView.beginUpdates()
tableView.deleteRows(at: [IndexPath], with: UITableViewRowAnimation.top)
tableView.insertRows(at: [IndexPath], with: UITableViewRowAnimation.top)
tableView.endUpdates()

您需要包装
deleteRows
insertRows
调用
tableView.beginUpdates
tableView.endUpdates

或者使用
性能更新:完成:

编辑

好的,让我详细解释一些事情。:-)

首先,我认为您需要修改章节中的numberOfRowsInSection,因为您说过只要没有选择任何类别,章节0就应该显示所有类别,并且在选择一个类别后,它应该只显示那一行

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        // If no category is selected, we display all categories, otherwise only the selected one.
        return selectedCategory == nil ? categories.count : 1
    case 1:
        return items.count
    default:
        return 0
    }
}
其次,这是一个示例,说明了
didSelectRowAt
的外观。我将
selectedCategory
更改为
Int?
以存储所选类别的索引,而不是其名称。 如您所见,在第0节中,不属于所选类别的行被删除,而第1节被完全添加,因为它以前不存在

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedCategory == nil {
        // Save index on selected category
        selectedCategory = indexPath.row

        // set items array
        …

        // Animate change
        var indexPaths = [IndexPath]()
        for (index, _) in categories.enumerated() {
            if index != selectedCategory {
                indexPaths.append(IndexPath(row: index, section: 0))
            }
        }

        tableView.performBatchUpdates({
            // delete rows from section 0
            tableView.deleteRows(at: indexPaths, with: .automatic)
            // insert section 1
            tableView.insertSections(IndexSet(integer: 1), with: .automatic)
        }, completion: nil)
    }
}

您需要包装
deleteRows
insertRows
调用
tableView.beginUpdates
tableView.endUpdates

或者使用
性能更新:完成:

编辑

好的,让我详细解释一些事情。:-)

首先,我认为您需要修改章节中的numberOfRowsInSection,因为您说过只要没有选择任何类别,章节0就应该显示所有类别,并且在选择一个类别后,它应该只显示那一行

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    switch section {
    case 0:
        // If no category is selected, we display all categories, otherwise only the selected one.
        return selectedCategory == nil ? categories.count : 1
    case 1:
        return items.count
    default:
        return 0
    }
}
其次,这是一个示例,说明了
didSelectRowAt
的外观。我将
selectedCategory
更改为
Int?
以存储所选类别的索引,而不是其名称。 如您所见,在第0节中,不属于所选类别的行被删除,而第1节被完全添加,因为它以前不存在

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if selectedCategory == nil {
        // Save index on selected category
        selectedCategory = indexPath.row

        // set items array
        …

        // Animate change
        var indexPaths = [IndexPath]()
        for (index, _) in categories.enumerated() {
            if index != selectedCategory {
                indexPaths.append(IndexPath(row: index, section: 0))
            }
        }

        tableView.performBatchUpdates({
            // delete rows from section 0
            tableView.deleteRows(at: indexPaths, with: .automatic)
            // insert section 1
            tableView.insertSections(IndexSet(integer: 1), with: .automatic)
        }, completion: nil)
    }
}

谢谢你的回答。但是我正在努力让indexPath插入和删除,你能给我们看看你的插入和删除代码吗?@Manish_Nainwal你应该用
cellForRow
中的所有
Indepath
来维护一个数组。从这个数组中,您可以得到
indepath
,谢谢您的回答。但是我正在努力让indexPath插入和删除,你能给我们看看你的插入和删除代码吗?@Manish_Nainwal你应该用
cellForRow
中的所有
Indepath
来维护一个数组。从这个数组中,您可以得到
indepath
,谢谢您的回答。但我正在努力让indexpaths插入和删除我的答案中添加了一些细节。谢谢你的回答。但我正在努力让indexpaths插入和删除我的答案中添加了一些细节。嗯。