如何建立具有可折叠部分的表格视图?

时间:2019-12-05 17:15:07

标签: ios swift xcode uitableview

我做了一个表格视图,但是我只希望当我触摸它时最后一部分会折叠。 这里的问题是有时其他部分也会崩溃。 怎么了?

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
    if indexPath.row == 0 {
        pushVentasTab() // pushVentaArticulos()
    } else if indexPath.row == 1 {
        pushReporteDDia()
    } else if indexPath.row == 2 {

        if SelectedIndex == indexPath.row
        {

            if self.isCollapce == false
            {
                self.isCollapce = true

            }else
            {
                self.isCollapce = false
            }
        }else{
            self.isCollapce = true
        }
    }
    self.SelectedIndex = indexPath.row
    tableView.reloadRows(at: [indexPath], with: .automatic)
}

1 个答案:

答案 0 :(得分:0)

看看您的代码,我假设您实际上是指tableView中的最后吗?我会使用deleteRows(at:with:)。见下文。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)

    //Check if last row
    if indexPath.row == dataSourceArray.count - 1 {
           tableView.deleteRows(at: [indexPath], with: .none)
    }
}

dataSourceArray是用于填充cellForRowAt中单元格的数组。

希望有帮助。