swift 可扩展表视图错误

ldioqlga  于 2023-03-11  发布在  Swift
关注(0)|答案(2)|浏览(103)

我有可扩展的表格视图单元格,除animation外,所有单元格都正常工作。我有一个带有UISwitch的单元格,当我点击它时,其他单元格出现,但有一些视图移动,当我点击UISwitch时隐藏这些单元格也是如此。
我使用的是insetGroup表格视图,这些移动使视图变成方形而不是圆形。
我想保持动画,我尝试了reloadSections(IndexSet(integer: 0), with: .fade)似乎是一个解决方案,但它也重新加载我的头部,但我不想这样。
我的单元格行函数:

internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "RemindersCell", for: indexPath) as! RemindersCell
    
    cell.switchReminder.isOn = remindersAllowed ? true : false
    cell.switchReminder.addTarget(self, action: #selector(switchTapped(sender:)), for: .touchUpInside)
    
    return cell
}

我的节中行数函数:

internal func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return remindersAllowed ? 5 : 1
}

我的UISwitch功能:

@objc private func switchTapped(sender: UISwitch) {
    
    if !remindersAllowed {
        // Add
        remindersAllowed = true
    } else {
        // Delete
        remindersAllowed = false
    }

    tableView.beginUpdates()
    tableView.reloadSections(IndexSet(integer: 0), with: .none)
    tableView.endUpdates()
}

默认remindersAllowed是真的,当我切换它变成假的,隐藏单元格。我真的不明白是什么问题,任何帮助将不胜感激!
当我隐藏单元格时,gif显示了这个错误。

bvjxkvbb

bvjxkvbb1#

你可以使用激活单元格作为表格视图头

ctrmrzij

ctrmrzij2#

您可以尝试使用以下代码删除系统重载部分动画并使用fade动画重载部分:

tableView.reloadData()
tableView.reloadSections(IndexSet(integer: 0), with: .fade)

相关问题