ios - 快速单击 UITableView 中的单元格时折叠部分

标签 ios swift uitableview

我有一个 UITableView,其中部分的数量和这些部分下的单元格的数量来 self 的服务。部分的数量是动态的,并且其下包含动态单元格。取决于来自服务的数据。我想在单击单元格时折叠部分。单击每个部分下面的单元格时,每个部分都应该折叠。我怎样才能把它折叠起来?我已经搜索了很多,但大多数结果都是在我们点击它展开和折叠的部分时显示的。但我想在单击单元格时折叠部分。我将数据放置在节标题中的代码是这样的,

func numberOfSections(in tableView: UITableView) -> Int {

    if tableView == resMenuTableView{

        return subCategoryModel!.count
    }
    else
    {
        return AddonCategoryModel!.count
    }
}
 func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    if tableView == resMenuTableView{

        return subCategoryModel![section].subCatName

    }
    else
    {
         return AddonCategoryModel![section].name
    }
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if tableView == resMenuTableView{
        return subCategoryModel![section].items.count
    }
    else
    {
        return AddonCategoryModel![section].addonItems.count
    }
}

最佳答案

当您单击单元格时,您将从 indexPath.section 中获取部分。您可以更新 viewState 以设置部分折叠标志,然后重新加载表。设置了折叠标志的部分为该部分分配行数 0。

如果表格中一次只有一个部分折叠,那么您将很容易处理逻辑。

关于ios - 快速单击 UITableView 中的单元格时折叠部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50396667/

相关文章:

ios - 遍历数组并在 Swift 中增加计数器

ios - 从 Firestore Swift 4 iOS 检索最后一个文档 ID

swift - 转换 NSData 中的 CKRecord

ios - 如何阅读通知中的文字?

ios - 核心情节: padding for the graph

ios - 将变量结果从一个 View Controller 传递到另一个非 View Controller swift 文件

json - EXC_BAD 在 swift 中将 Any 转换为 Int 时的指令

ios - UITableViewCell 内的 UITableView - didSelectRowAtIndexPath

ios - UItableviewcell "cell-identifier"内存管理

iphone - UITableView 节标题 1px 光泽/阴影重叠